C# 欧拉旋转问题

C# 欧拉旋转问题,c#,euler-angles,C#,Euler Angles,我正在用C#编写一个简单的代码,以确保玩家模型的头部指向鼠标的矢量3位置(lookPoint),但它夹在90度范围内,与躯干当前方向的任一侧成45度 我已经研究了euler角度的结果,以确保得到y轴所需的旋转值,但我很难确定euler角度何时应再次循环到0,我似乎不知道如何进行排序 minRot=mytorson.transform.rotation.eulerAngles.y-180f-45f; maxRot=我的躯干。变换。旋转。欧拉格朗日。y-180f+45f; lookDirection

我正在用C#编写一个简单的代码,以确保玩家模型的头部指向鼠标的矢量3位置(lookPoint),但它夹在90度范围内,与躯干当前方向的任一侧成45度

我已经研究了euler角度的结果,以确保得到y轴所需的旋转值,但我很难确定euler角度何时应再次循环到0,我似乎不知道如何进行排序

minRot=mytorson.transform.rotation.eulerAngles.y-180f-45f;
maxRot=我的躯干。变换。旋转。欧拉格朗日。y-180f+45f;
lookDirection=Mathf.Atan2(lookPoint.x-transform.position.x,lookPoint.z-transform.position.z);
lookRotation=Mathf.夹具(Mathf.Rad2Deg*lookDirection,minRot,maxRot);
myHead.eulerAngles=新向量3(0,lookRotation,0);
这会导致头部在无法确定其最大值或最小值时反弹到极端值之一


有谁能帮我定义minRot和maxRot,这样它就可以解释180度交叉了吗?

这应该可以满足您的要求。我只是基于提供的变量和代码,所以有可能事情不能完美地工作。如果没有,请告诉我,我们可以调整:

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;
lookDirection=Mathf.Atan2(lookPoint.x-transform.position.x,
lookPoint.z-变换.position.z)*Mathf.Rad2Deg;
四元数q=Quaternion.Euler(新向量3(0,lookDirection,0));
四元数目标=新四元数();
四元数旋转=我的躯干.transform.rotation;
//检查角度是否超出45度范围

if(Quaternion.Angle(q,torsoRotation)这应该满足您的要求。我只是根据提供的变量和代码进行设置,所以可能无法完美运行。如果不能,请告诉我,我们可以调整:

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;
lookDirection=Mathf.Atan2(lookPoint.x-transform.position.x,
lookPoint.z-变换.position.z)*Mathf.Rad2Deg;
四元数q=Quaternion.Euler(新向量3(0,lookDirection,0));
四元数目标=新四元数();
四元数旋转=我的躯干.transform.rotation;
//检查角度是否超出45度范围

if(四元数。角度(q,torsoRotation)谢谢!这非常有效!我通过硬编码各种if语句来检查范围,得到了一个“解决方案”,但这明显更加优雅和简洁。谢谢!没问题,很高兴我能帮上忙。谢谢!这非常有效!我得到了一个“解决方案”通过硬编码各种if语句来检查范围,但这明显更加优雅和简洁。谢谢!没问题,很高兴我能提供帮助。