MouseOrbit 自定义:将相机放置在定点轨道上

发布于 2024-11-18 06:47:57 字数 1142 浏览 2 评论 0原文

我有一个基于标准资源脚本的 MouseOrbit 脚本,我需要对其进行自定义以将相机放置在轨道中的特定位置。

以下是 Unity3d 附带的标准脚本的基础知识:

函数 Start () {

    var 角度=transform.eulerAngles;
    x = 角度.y;
    y = 角度.x;

    // 使刚体不改变旋转
    如果(刚体)
        刚体.freezeRotation = true;   
}

函数 onUpdate(){

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

    var 旋转 = Quaternion.Euler(y, x,0);
    var 位置 = 旋转 * Vector3(0.0, 0.0,cameraDelta); 

    变换.旋转 = 旋转;
    变换.位置 = 位置; 
}

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

第一个位于物体的正后方。 x:7,:y0,z:0

这是我认为可行的:

函数 TransformCamera(x,y,z){

    //设置相机位置
    变换.position = new Vector3(x, y, z);

    var 角度=transform.eulerAngles;
    y = 角度.y;
    x = 角度.x;
    z = 角度.z;

    var 旋转 = Quaternion.Euler(y, x, z);
    var 位置 = 旋转 * Vector3(0.0, 0.0,cameraDelta);
    //调整后的目标;

    变换.旋转 = 旋转;
    变换.位置 = 位置;
}

这个脚本很接近......它变换相机并旋转它以查看对象,但它没有将相机放置在正确的位置7,0,0

谢谢!

I have a MouseOrbit script based on the Standard Assets Script that I need to customize to place the camera in a specific spot in the orbit.

Heres the basics of the standard script that ships with 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; 
}

What I need to do is place the camera in a few spots around the target object at 0,0.

The first is directly behind the object. x:7,:y0,z:0.

Here's what I thought would work:

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;
}

This script is close... it transforms the camera and rotates it to look at the object, but it doesn't place the camera in the correct location 7,0,0.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(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

Try this modified script. i hope this will solve your problem.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文