【问题标题】:Clamp RotateAround is not working on Camera in Unity3DClamp RotateAround 不适用于 Unity3D 中的相机
【发布时间】:2016-10-04 12:16:02
【问题描述】:

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

【问题讨论】:

  • transform.RotateAround 实际上会旋转变换,因此每次更新都会旋转,然后使用 transform.localEulerAngles 再次旋转。你想让相机做什么?
  • 我希望相机将 Y 轴夹在 10 和 -10,X 轴夹在 45 和 -45
  • 如果我删除最后 3 行,它会按照我的意愿完美旋转。但我想夹住所以我添加了最后 3 行但它没有夹住。
  • 好的,所以不要先使用 transform.RotateAround 计算你的角度,夹紧它们,然后使用 transform.rotation = 不管。您可以在 Quaternion 和 Vector3 类中使用各种方法来帮助您处理角度
  • 实际上,我对此很陌生,您可以更正代码并发布答案吗?我会接受答案。顺便感谢您的快速回复:) @Absinthe

标签: android unity3d camera clamp


【解决方案1】:

这是一个限制 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);

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      相关资源
      最近更新 更多