【问题标题】:Rotating Camera around a player object on both X and Y axis with mouse button使用鼠标按钮在 X 和 Y 轴上围绕玩家对象旋转相机
【发布时间】:2021-06-23 00:31:39
【问题描述】:

我正在尝试使用鼠标中键在 X 轴和 Y 轴上旋转我的第三人称玩家游戏对象的摄像机角度。

我在下面有这段代码。

public class CameraControl : MonoBehaviour
{
    public Transform player;
    public Vector3 offset;

    public float pitch = 2f;
    private float turnSpeed = 5f;
    private float currentZoom = 10f;

    void LateUpdate()
    {

        if (Input.GetMouseButton(2))
        {
            offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
            offset = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offset;
        }

        transform.position = player.position - offset * currentZoom;
        transform.LookAt(player.position + Vector3.up * pitch);

    }

}

单轴旋转似乎按预期工作,我可以沿 X 轴或 Y 轴旋转而没有任何问题,但是当它们混合在一起(同时在两个方向上移动)时,有些东西似乎会破裂并且旋转似乎也不起作用。就好像它在途中失去了沿 Y 轴移动的能力。

另一个问题是我不太确定如何限制 Y 轴,以使旋转不会低于地面或高于和围绕玩家的头部。我考虑过使用 Mathf.Clamp,但在我的代码中设置最小值和最大值似乎太成问题了。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    对于Quaternion operator *,订单很重要!是currentRotation * additionalRotation

    通常也是为了不发生倾斜旋转而您想要做的事情

    • 围绕全局 Y 轴旋转
    • 围绕局部 X 轴旋转

    我觉得应该是这样的

    if (Input.GetMouseButton(2))
    {
        offset *= Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up);
        offset *= Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * turnSpeed, transform.right);
    }
    

    总的来说,我会提出一种不同的方法

    我会给相机一个父对象并将该父对象放置在要查看的位置(您的播放器)。

    然后将相机置于负局部 Z 轴(缩放)上。

    对于旋转,您更愿意简单地旋转父对象。

    层次结构

     - CameraAnchor
     |--Camera (With CameraControl)
    

    然后做类似的事情

    public class CameraControl : MonoBehaviour
    {
        public Transform player;
        public Vector3 offset;
    
        public float pitch = 2f;
        private float turnSpeed = 5f;
        private float currentZoom = 10f;
    
        void LateUpdate()
        {
            // Center the parent on the player with an optional offset
            transform.parent.position = player.position + offset;
    
            if (Input.GetMouseButton(2))
            {
                // Simply rotate the parent accordingly
                // Since the camera is a child of it it will automatically be moved in an orbit
                transform.parent.rotation *= Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up);
                transform.parent.rotation *= Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * turnSpeed, transform.parent.right);
            }
    
            // For the zoom simply move the camera in its local space forward and backward
            // A higher value for currentZoom means further away 
            // so you might want to either change the name or use something like 1/currentZoom
            transform.localPosition = Vector3.back * currentZoom;
        }
    }
    

    【讨论】:

    • 我对您提供的代码的问题是旋转使相机完全倾斜,而不是停留在 X 轴上。有没有可能的解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    相关资源
    最近更新 更多