【发布时间】:2020-11-18 12:51:45
【问题描述】:
我想将箭头移动的区域限制在 -140 到 -40 度之间,如图所示,但是如果我将其限制在 -140 到 -40 度之间,角度区域的度数分布很奇怪分别是最小值和最大值,它只让我在红色区域移动箭头,而不是在所需的区域,我的意思是我只想在非红色区域移动箭头,如果有人可以帮助我从这段代码开始而不改变非常感谢,谢谢。
编辑:添加了基于条件的临时代码。
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
//rotationZ = Mathf.Clamp(rotationZ, -140, 180);
if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
Debug.Log(rotationZ);
if (rotationZ < -90 || rotationZ > 90)
{
if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
if (myPlayer.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
}
else if (myPlayer.transform.eulerAngles.y == 180)
{
transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
}
}
}
【问题讨论】:
-
它是3D游戏吗?功能就像在Rust中按Alt环顾四周,所以角色不会向后看180度?
-
我不喜欢这段代码,但至少你可以使用
if(rotationZ <= -90 && rotationZ > -140) {rotationZ = -140;} if(rotationZ > -90 && rotationZ < -40) {rotationZ = -40;} -
@Whitebrim 你会做什么呢?我有兴趣学习一种更有效的方法。我附上了完整的代码,因为还有一部分我改变了图像的旋转,使其成为镜像版本。
-
将 -140 改为 220
-
@Antoine 是的,但它是一个圆度方程,只是一个简单的技巧,Unity 通过加减 360 度来转换它,这意味着 220 等于 -140,这个转换将使你的箭头在非红色区域从 -140 移动到 -40。试试吧 rotationZ = Mathf.Clamp(rotationZ, -40, 220);
标签: visual-studio unity3d rotation 2d euler-angles