【问题标题】:Unity head Y axis rotation clamp统一头Y轴旋转夹具
【发布时间】:2017-08-18 22:18:07
【问题描述】:

我正在尝试制作一个统一的 FPP 模式,您可以在其中看到您的实际身体。我创建了一个模型,操纵了一切。我的头会旋转到相机,但我不希望玩家能够围绕他的身体旋转。我已经在 x 轴上夹紧了旋转,但是在围绕 y 轴夹紧时遇到了问题。

void Update () {
currentBodyRotation = body.GetComponent<Transform> ().rotation.eulerAngles.y;

    yaw += Input.GetAxis ("Mouse X") * mouseSensitivity;


    yawMin = currentBodyRotation - 90f;
    yawMax = currentBodyRotation + 90f;

    yaw =  Mathf.Clamp (yaw, yawMin, yawMax);



    pitch -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
    pitch = Mathf.Clamp (pitch, pitchMinMax.x, pitchMinMax.y);


    currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3 (pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
    transform.rotation = Quaternion.Euler (currentRotation);

}

我认为旋转在 0 角和 360 角时有限制。我的代码完美运行,直到身体达到 360 度。当它发生时,虽然我的相机会猛拉,然后从看不见的墙上“弹回”到它来自的一侧。

【问题讨论】:

  • 尝试使用this 进行摄像头限制。那应该可以。
  • 不起作用。 transform.localEulerAngles 搞砸了我的相机和动画。编辑:我已经修复它但仍然无法正常工作。它停止获取我的部分鼠标输入。
  • 认为您在移动相机时有一定的角度限制。该代码适用于 FPS 相机,应附加到没有动画的相机。
  • 现在几乎一切正常,但我仍然需要弄清楚如何将相机旋转到适当的位置。每次我启动测试时,我的相机都会围绕 Z 轴旋转 90 度。

标签: c# unity3d


【解决方案1】:

好的。我已经想通了。如果有人有类似的问题,这是代码,也许它也适用于他们。

void Update () {

    yaw += Input.GetAxis ("Mouse Y") * mouseHorizontalSensitivity;


    yaw =  Mathf.Clamp (yaw, -30f, 80f);  // It's pitch but my code works weird



    pitch -= Input.GetAxis ("Mouse X") * mouseVerticalSensitivity;
    pitch = Mathf.Clamp (pitch, -90f, 90f);  // it's yaw


    currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3 (-pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
    transform.localEulerAngles = currentRotation;

// my mouse input wouldn't go down which caused the model to rotate indeffinitely so i added smoothing to 0 for rotating around Y axis which here is labeled as pitch
    if (!Input.GetKey (KeyCode.LeftAlt))
        pitch = Mathf.SmoothDamp(pitch, 0f, ref rotationSmoothVelocity2, rotationSmoothTime); 

}

【讨论】:

    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多