2011-06-29 13 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

関連する問題