2

我的代码不起作用,我试图夹住相机,但它不起作用。它立即捕捉到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);
         }
     }
 }
4

2 回答 2

0

这是限制 Y 轴旋转的示例,您也可以将其调整为 X。您没有说明限制应该基于什么,所以这里它基于另一个对象(公共变换目标)的旋转,这可能是您的播放器或其他对象。

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

    }
}

相反,如果您想根据世界空间进行限制,只需检查相机的旋转:

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);
于 2016-10-05T06:48:28.963 回答
0

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

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);
        }
    }
}
于 2016-10-06T06:00:33.150 回答