我正在用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度的交叉吗?
发布于 2019-05-19 08:22:18
这应该能做你想要的。我只是把它建立在提供的变量和代码的基础上,所以有可能事情不能完美地工作。如果没有,请告诉我,我们可以调整:
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;https://stackoverflow.com/questions/56203859
复制相似问题