我正在用C#编写简单的代码,以确保玩家模型的头部指向鼠标的Vector3位置(lookPoint),但要在90度范围内(夹在躯干两侧的45度之间)当前方向。
我一直在尝试使用欧拉角的结果来确保获得y轴的所需旋转值,但是当欧拉角应再次循环到0时我却感到困惑,而我无法似乎想出如何解决它。
minRot = myTorso.transform.rotation.eulerAngles.y-180f-45f;
maxRot = myTorso.transform.rotation.eulerAngles.y-180f+45f;
lookDirection = Mathf.Atan2(lookPoint.x - transform.position.x, lookPoint.z - transform.position.z);
lookRotation = Mathf.Clamp(Mathf.Rad2Deg * lookDirection, minRot, maxRot);
myHead.eulerAngles = new Vector3(0,lookRotation,0);
这导致头部无法确定最大值或最小值时会突然回到极端状态。
有人可以帮我定义minRot和maxRot,以便它们说明180度交叉吗?
答案 0 :(得分:0)
这应该可以满足您的需求。我只是基于提供的变量和代码,因此有可能事情可能无法完美运行。所以让我知道是否可以,我们可以进行调整:
lookDirection = Mathf.Atan2(lookPoint.x - transform.position.x,
lookPoint.z - transform.position.z) * Mathf.Rad2Deg;
Quaternion q = Quaternion.Euler(new Vector3(0, lookDirection, 0));
Quaternion targetRotation = new Quaternion();
Quaternion torsoRotation = myTorso.transform.rotation;
// Check if the angle is outside the 45degree range
if (Quaternion.Angle(q, torsoRotation) <= 45.0f)
targetRotation = q;
else
{
// This is to check which direction we're out of range
float d = Mathf.DeltaAngle(q.eulerAngles.y, torsoRotation.eulerAngles.y);
if (d > 0.0f)
target = torsoRotation * Quaternion.Euler(0, -45f, 0);
else if (d < 0.0f)
target = torsoRotation * Quaternion.Euler(0, 45f, 0);
}
myHead.rotation = targetRotation;