2011-06-29 31 views
1

我有一个基于标准资产脚本的MouseOrbit脚本,我需要自定义将相机放置在轨道中的特定位置。标准脚本MouseOrbit自定义:将相机放置在现场轨道上

继承人的基础附带Unity3d:

function Start() { 

    var angles = transform.eulerAngles; 
    x = angles.y; 
    y = angles.x; 

    // Make the rigid body not change rotation 
    if (rigidbody) 
     rigidbody.freezeRotation = true; 
} 

function onUpdate(){ 

    x += Input.GetAxis("Mouse X") * xSpeed; 
    y -= Input.GetAxis("Mouse Y") * ySpeed; 

    var rotation = Quaternion.Euler(y, x,0); 
    var position = rotation * Vector3(0.0, 0.0, cameraDelta); 

    transform.rotation = rotation; 
    transform.position = position; 
} 

我需要做的是放置在0,0围绕目标对象的几个景点的相机。

第一个直接在对象后面。 x:7,:y0,z:0

这里就是我想会的工作:

function TransformCamera(x,y,z){ 

    //set position of camera 
    transform.position = new Vector3(x, y, z); 

    var angles = transform.eulerAngles; 
    y = angles.y; 
    x = angles.x; 
    z = angles.z; 

    var rotation = Quaternion.Euler(y, x, z); 
    var position = rotation * Vector3(0.0, 0.0, cameraDelta); 
    //adjusted_target; 

    transform.rotation = rotation; 
    transform.position = position; 
} 

这个脚本是关闭...它把相机和旋转,它看起来在对象,但它不会把相机在正确的位置7,0,0

谢谢!

回答

1

试试这个修改后的脚本。我希望这会解决你的问题。

using UnityEngine; 
using System.Collections; 

public class OrbitCamera : MonoBehaviour 
{ 

    //The target of the camera. The camera will always point to this object. 
    public Transform _target; 

    //The default distance of the camera from the target. 
    private float _distance ; 

    //Control the speed of zooming and dezooming. 
    public float _zoomStep = 1.0f; 

    //The speed of the camera. Control how fast the camera will rotate. 
    public float _xSpeed = 1f; 
    public float _ySpeed = 1f; 

    //The position of the cursor on the screen. Used to rotate the camera. 
    private float _x = 0.0f; 
    private float _y = 0.0f; 

    //Distance vector. 
    private Vector3 _distanceVector; 

    /** 
    * Move the camera to its initial position. 
    */ 
    void Start() 
    { 
     _distanceVector = new Vector3(0.0f,0.0f,-this.transform.position.z); 
     _distance = this.transform.position.z; 
     Vector2 angles = this.transform.localEulerAngles; 
     _x = angles.x; 
     _y = angles.y; 

     this.Rotate(_x, _y); 
    } 

    /** 
    * Rotate the camera or zoom depending on the input of the player. 
    */ 
    void LateUpdate() 
    { 
     if (_target) 
     { 
      this.RotateControls(); 
      this.Zoom(); 
     } 
    } 

    /** 
    * Rotate the camera when the first button of the mouse is pressed. 
    * 
    */ 
    void RotateControls() 
    { 
     if (Input.GetButton("Fire1")) 
     { 
      _x += Input.GetAxis("Mouse X") * _xSpeed; 
      _y += -Input.GetAxis("Mouse Y")* _ySpeed; 

      this.Rotate(_x,_y); 
     } 
    } 

    /** 
    * Transform the cursor mouvement in rotation and in a new position 
    * for the camera. 
    */ 
    void Rotate(float x, float y) 
    { 
     //Transform angle in degree in quaternion form used by Unity for rotation. 
     Quaternion rotation = Quaternion.Euler(y,x,0.0f); 

     //The new position is the target position + the distance vector of the camera 
     //rotated at the specified angle. 
     Vector3 position = rotation * _distanceVector + _target.position; 

     //Update the rotation and position of the camera. 
     transform.rotation = rotation; 
     transform.position = position; 
    } 

    /** 
    * Zoom or dezoom depending on the input of the mouse wheel. 
    */ 
    void Zoom() 
    { 
     if (Input.GetAxis("Mouse ScrollWheel") < 0.0f) 
     { 
      this.ZoomOut(); 
     } 
     else if (Input.GetAxis("Mouse ScrollWheel") > 0.0f) 
     { 
      this.ZoomIn(); 
     } 

    } 

    /** 
    * Reduce the distance from the camera to the target and 
    * update the position of the camera (with the Rotate function). 
    */ 
    void ZoomIn() 
    { 
     _distance -= _zoomStep; 
     _distanceVector = new Vector3(0.0f,0.0f,-_distance); 
     this.Rotate(_x,_y); 
    } 

    /** 
    * Increase the distance from the camera to the target and 
    * update the position of the camera (with the Rotate function). 
    */ 
    void ZoomOut() 
    { 
     _distance += _zoomStep; 
     _distanceVector = new Vector3(0.0f,0.0f,-_distance); 
     this.Rotate(_x,_y); 
    } 

} //End class 
+0

谢谢它适合我。 – Neeraj