【问题标题】:Which rotation is shown in the Inspector?检查器中显示了哪个旋转?
【发布时间】:2019-02-07 21:29:17
【问题描述】:

我的玩家的胸骨可以在瞄准时旋转。

现在我想评估我应该让胸部旋转多少(最小和最大旋转)。

为此,我允许所有角度的旋转并查看检查器。

例如,胸部应可向左旋转的最小值应为 Y=-15。 在 Y=-15(在 Inspector 中看到),它看起来仍然很自然。

现在我想编写这个代码。

令我惊讶的是,chest.localRotation.Y 与 Inspector 显示的值完全不同。

然后我查看了胸部变量并扩展了视图。 我只是看不到 Inspector 显示的旋转值。

请问在这种情况下我该怎么办?

我正在使用它来旋转骨骼:

Chest.LookAt(ChestLookTarget.position); 
Chest.rotation = Chest.rotation * Quaternion.Euler(Offset);

谢谢!

【问题讨论】:

  • 这是localRotation .. 或者更好的说法是Quaternion 值的许多可能的欧拉角表示之一。所以transform.localRotation = Quaternion.EulerAngles(17.815f, -15.395f, 0.746f);transform.localRotation = Quaternion.EulerAngles(5.0f, 316.9f, 352.4f) 应该有相同的结果
  • 感谢您提供的信息。你能建议我应该如何处理这个吗?我的意思是我需要以某种方式限制旋转。 :-)
  • 你可以试试if (transform.localRotation.Y < value) { transform.localRotation.Y = value;}
  • @nothing 不,OP 已经说过他尝试过,并且.. 阅读和设置 Quaternion 值通常不是一个好主意! Quaternions 有 4 个值 xyzw,它们的总和始终为 1

标签: unity3d


【解决方案1】:

不起作用的原因:

Quaternion 不是人类可读的值。

一个Quaternion 总是唯一的,但在Euler 空间中可以有多个(无限?)不同的表示!反之,一个 Euler 始终代表一个 Quaternion 值。

如果您查看它明确说明的文档

除非您完全了解四元数,否则不要直接修改它。


正如您在检查器中看到的那样,localRotation 与父级 Transform 相关。

最好说它是导致Quaternion 的众多可能的Euler 输入之一。您在localEulerAngles 的调试中看到的是另一种可能的Euler 表示。 Unity 通常在 localEulerAngles 中也只为您提供值 > 0


好像箱子反正只会绕Y轴旋转吧?

如果是这种情况,您可以简单地获得胸部原始前向矢量与目标之间的角度。处理Vector3 值比处理Quaternions 容易得多;)

似乎和this post中的用例一样

// get the target direction
Vector3 targetDir = ChestLookTarget.position - Chest.position;

// Reset any difference in the Y axis
// since it would change the angle as well if there was a difference I the height
// between the two objects
targetDir.y = 0;

// however you currently rotate
// instead rotate only the Vector3 variable without applying it to the transform yet
Vector3 newDir = Vector3.RotateTowards(Chest.forward, targetDir, RotationSpeed * Time.deltaTime, 0.0f);

// Compare the target direction to the parents forward vector
float newAngle = Vector3.Angle(Chest.parent.transform.forward, newDir);

if (newAngle > MaxRotationAngle)
{
    // What should happen if angle gets bigger?
    return;
}

// If angle still okey set the new direction
Chest.rotation = Quaternion.LookRotation(newDir);

【讨论】:

  • 胸骨也应该可以上下旋转。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 2012-04-02
相关资源
最近更新 更多