简体   繁体   English

在Unity中夹紧垂直旋转

[英]clamping a vertical rotation in Unity

I started a FPS project. 我开始了FPS项目。 I have a capsule as a Player, an empty GO as the head and the camera is the child object of the head. 我有一个胶囊作为播放器,头上有一个空的GO,相机是头的子对象。

On the vertical Axis, I clamp my y-rotation to -70 (min) and 70 (max). 在垂直轴上,我将y旋转限制为-70(最小)和70(最大)。 Surprisingly the value does not get clamped. 令人惊讶的是,该值没有被钳位。

    [SerializeField]
    private Transform playerHead;

    float inputHorizontal;
    float inputVertical; 

    float sensitivity = 50;

    float yMin = -70;
    float yMax = 70;

    Vector2 direction; // movement direction

Transform playerTransform;

void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}

    private void Update()
    {
        inputHorizontal = Input.GetAxisRaw("Mouse X");
        inputVertical = -Input.GetAxisRaw("Mouse Y"); // Inverted Input!

        direction = new Vector2(
                inputHorizontal * sensitivity * Time.deltaTime,
                Mathf.Clamp(inputVertical * sensitivity * Time.deltaTime, yMin, yMax)); // The horizontal movement and the clamped vertical movement

        playerTransform.Rotate(0, direction.x, 0);
        playerHead.Rotate(direction.y, 0, 0);
    }

The value 价值

direction.y

gets clamped but I can still turn around my head by 360 degrees.. 被夹住,但我仍然可以绕头转动360度。

You are clamping your delta rotation - not your actual rotation. 您正在夹紧增量旋转-而不是实际旋转。 Said in another way, direction is not the final rotation, it is the change in rotation. 换句话说, direction不是最终的旋转,而是旋转的变化。 What you are effectively doing, is limiting the rotation to 70 degrees per frame. 您正在有效地做的是将旋转限制为每帧70度。

You probably want to limit the actual rotation of playerHead , for example by adding the following lines to the end of update: 您可能想限制playerHead的实际旋转,例如通过在更新结束时添加以下几行:

Vector3 playerHeadEulerAngles = playerHead.rotation.eulerAngles;
playerHeadEulerAngles.y = Mathf.Clamp(playerHeadEulerAngles.y, yMin, yMax);
playerHead.rotation = Quaternion.Euler(playerHeadEulerAngles);

There is also no reason to create a direction vector, when you are using each component separately anyway. 无论如何,当您分别使用每个组件时,也没有理由创建方向矢量。

Clamp the final Y direction value not the current mouse movement - 夹紧最终的Y方向值而不是当前的鼠标移动-

[SerializeField]
private Transform playerHead;

float inputHorizontal;
float inputVertical; 

float sensitivity = 50;

float yMin = -70;
float yMax = 70;

Vector2 direction; // movement direction
float currYDir = 0,prevYDir = 0;

Transform playerTransform;

void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}

private void Update()
{
    inputHorizontal = Input.GetAxisRaw("Mouse X");
    inputVertical = -Input.GetAxisRaw("Mouse Y"); // Inverted Input!

    direction = new Vector2(
            inputHorizontal * sensitivity * Time.deltaTime,
            inputVertical * sensitivity * Time.deltaTime); // The horizontal movement and the clamped vertical movement

    playerTransform.Rotate(0, direction.x, 0);

    currYDir = prevYDir + direction.y;
    currYDir = Mathf.Clamp(currYDir, yMin, yMax);
    playerHead.Rotate(currYDir-prevYDir, 0, 0);

    prevYDir = currYDir;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM