Clamp RotateAround 不适用于 Unity3D 中的相机

Clamp RotateAround is not working on Camera in Unity3D

我的代码不工作,我试图夹住相机,但它不工作。它立即捕捉到 45。我怎样才能夹住相机?

这是我的代码。

using UnityEngine;
using System.Collections;

 public class MoveCamera : MonoBehaviour 
 {

     public float sensitivity = 4.0f;        
     private Vector3 mouseOrigin;
     private bool isRotating;

     private float minX = -45.0f;
     private float maxX = 45.0f;

     private float minY = -10.0f;
     private float maxY = 10.0f;

     float rotationY = 0.0f;
     float rotationX = 0.0f;

     void Start()
     {

     }

     void Update () 
     {

         if (Input.GetMouseButtonDown (0)) {

             mouseOrigin = Input.mousePosition;
             isRotating = true;
         }

         if (!Input.GetMouseButton (0))
             isRotating = false;

         if (isRotating) {

             Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin);
             transform.RotateAround (transform.position, transform.right, -pos.y * sensitivity);
             transform.RotateAround (transform.position, Vector3.up, pos.x * sensitivity);

             rotationY = Mathf.Clamp (transform.localEulerAngles.y, minY, maxY);
             rotationX = Mathf.Clamp (transform.localEulerAngles.x, minX, maxX);
             transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
         }
     }
 }

这里有一个限制Y轴旋转的例子,你也可以适配X轴。你没有说明限制应该基于什么,所以这里它基于另一个对象的旋转(public 变换目标),这可能是你的玩家或其他任何东西。

public float sensitivity = 16.0f;
public Transform target;

void Update()
{

    if (Input.GetMouseButton(0))
    {
        //Debug.Log(Quaternion.Angle(transform.rotation, target.rotation));
        float angle = Quaternion.Angle(transform.rotation, target.rotation);
        if(angle < 45 || angle > 315)
        {
            Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
            transform.RotateAround(pos, Vector3.up, Time.deltaTime * sensitivity);
        }

    }
}

如果你想根据世界限制 space 只需检查相机的旋转:

public float sensitivity = 16.0f;
//public Transform target;

void Update()
{

    if (Input.GetMouseButton(0))
    {

        float angle = transform.eulerAngles.y;
        Debug.Log(angle);
        if (angle < 45 || angle > 315)
        {
            Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
            transform.RotateAround(pos, Vector3.up, Time.deltaTime * sensitivity);
        }

    }
}

请注意,在这两种情况下都使用 Time.deltaTime 来确保无论玩家的帧速率如何,旋转都以相同的速度发生。

如果要反转旋转,请反转 RotateAround 的轴参数:

transform.RotateAround(pos, -Vector3.up, Time.deltaTime * sensitivity);

我修好了。这是完整的代码。

using UnityEngine;
using System.Collections;

public class MoveCamera : MonoBehaviour 
{

    public float sensitivity = 4.0f;        
    private Vector3 mouseOrigin;
    private bool isRotating;
    public GameObject cam;

    void Start()
    {
    }

    protected float ClampAngle(float angle, float min, float max) {

        angle = NormalizeAngle(angle);
        if (angle > 180) {
            angle -= 360;
        } else if (angle < -180) {
            angle += 360;
        }

        min = NormalizeAngle(min);
        if (min > 180) {
            min -= 360;
        } else if (min < -180) {
            min += 360;
        }

        max = NormalizeAngle(max);
        if (max > 180) {
            max -= 360;
        } else if (max < -180) {
            max += 360;
        }

        return Mathf.Clamp(angle, min, max);
    }

    protected float NormalizeAngle(float angle) {
        while (angle > 360)
            angle -= 360;
        while (angle < 0)
            angle += 360;
        return angle;
    }


    void Update () 
    {

        if (Input.GetMouseButtonDown (0)) {

            mouseOrigin = Input.mousePosition;
            isRotating = true;
        }

        if (!Input.GetMouseButton (0))
            isRotating = false;

        if (isRotating) {

            cam.transform.localEulerAngles = new Vector3(0, ClampAngle(cam.transform.localEulerAngles.y, -45, 45), 0);
            Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin);
            transform.RotateAround (transform.position, transform.right, -pos.y * sensitivity);
            transform.RotateAround (transform.position, Vector3.up, pos.x * sensitivity);
        }
    }
}