【问题标题】:How to clamp camera in Unity3D如何在 Unity3D 中夹紧相机
【发布时间】:2016-10-06 14:04:29
【问题描述】:

我的代码不起作用,我试图夹住相机,但它不起作用。相机怎么夹?

using UnityEngine;
using System.Collections;

public class MoveCamera : MonoBehaviour 
{

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

    private float speed = 2.0f;

    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 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 (rotationY, minY, maxY);
                rotationX = Mathf.Clamp (rotationX, minX, maxX);
                transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
            }
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    旋转后,您忘记从变换中获取rotationX 和rotationY 的值。试试这个:

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

    【讨论】:

    • 当你说“它不工作”时,试着解释 什么 不工作 ;)
    • 它捕捉到一个位置并卡住了。甚至没有旋转单点。
    • 调试后 - Debug.Log (rotationX);它给 3 输出 0 0.006 45
    【解决方案2】:

    这是Mathf.Clamp的代码

    public static float Clamp(float value, float min, float max) {
        if (value < min) {
            value = min;
        } else if (value > max) {
            value = max;
        }
        return value;
    }

    如果您不确定调用在 .NET(Unity / Mono / 等)中的工作方式,请使用 IL 反向工具,例如 (EXTERNAL LINK)ILSPY
    根据您发布的代码和理解Mathf.Clamp 应该按预期工作,问题很可能存在于您的代码中,至少在某一时刻,例如这里:

    
    rotationY = Mathf.Clamp (rotationX, minY, maxY); //note it's rotation "X" instead of "Y"
    rotationX = Mathf.Clamp (rotationX, minX, maxX);

    如果这仍然没有解决问题,请使用 Debug.Log 查看变量值以找到错误的位置。
    如果你不能这样解决,你就会清楚地知道你不能做什么,并且可以发布一个更清晰的问题并期待一个清晰的答案。

    希望这会有所帮助!

    【讨论】:

    • 我不想使用外部工具,顺便说一下,我更正了代码,它的“Y”而不是“X”。感谢您的快速回复。 :)
    • 我指的外部工具确实是外部的。它对 Unity 完全没有影响……再说一次,如果我发布的修复仍然不够,请在您的代码中使用 Debug.Log 来查看您实际执行的操作而不是您想要的操作。同样,Mathf.Clamp 正在按预期工作 - 即使在您的代码中......
    【解决方案3】:

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

    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
      • 2022-10-15
      • 2021-06-08
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多