gpt4 book ai didi

c# - 如何在 unity 5 中限制鼠标输入的旋转?

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:43 26 4
gpt4 key购买 nike

我有一个非常基本的脚本可以将相机从鼠标位置移开,但我想将 z 轴的旋转限制为某些值。使用下面的代码一切正常,但相机可以在 z 轴上完全旋转,我想将其限制为 20 和 -40。我曾尝试使用 mathf.clamp,但没有用,当打印到控制台时,它只打印出 mathf.clamp 中最右边的值。我还尝试使用 if 语句来查看旋转是否超过限制,如果超过则重置它。但都没有用...我也查看了 Unity 的答案,但我不明白任何其他答案,有人可以告诉我该怎么做吗?

代码:

void Update () {
transform.rotation = Quaternion.Euler(0f, Input.mousePosition.x, Input.mousePosition.y);
}

最佳答案

这就是你夹住它的方式。

void Update()
{
float zRotation = Mathf.Clamp(Input.mousePosition.y, -40, 20);
transform.rotation = Quaternion.Euler(0f, Input.mousePosition.x, zRotation);
}

但我不认为它可以满足您的要求。鼠标位置在窗口坐标中给出,因此您永远不会有负值。您可能希望首先转换坐标,如下所示:

void Update()
{
float yRotation = (Input.mousePosition.x - Screen.width / 2) * 360 / Screen.width;
float zRotation = (Input.mousePosition.y - Screen.height / 2) * 180 / Screen.height;
zRotation = Mathf.Clamp(zRotation, -40, 20);
transform.rotation = Quaternion.Euler(0f, yRotation, zRotation);
}

关于c# - 如何在 unity 5 中限制鼠标输入的旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32792490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com