1

我有一个基于标准资产脚本的 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

谢谢!

4

1 回答 1

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
于 2013-12-28T21:50:51.567 回答