首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >欧拉旋转问题

欧拉旋转问题
EN

Stack Overflow用户
提问于 2019-05-19 00:25:48
回答 1查看 366关注 0票数 1

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

我使用了欧拉角的结果,以确保我得到了y轴想要的旋转值,但是我很难理解当欧拉角应该再次循环到0的时候,我似乎想不出如何将它排序。

代码语言:javascript
复制
 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度的交叉吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-19 08:22:18

这应该能做你想要的。我只是把它建立在提供的变量和代码的基础上,所以有可能事情不能完美地工作。如果没有,请告诉我,我们可以调整:

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56203859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档