Unity:旋转Gizmo,偏移量为45

时间:2018-06-14 20:54:48

标签: c# unity3d matrix quaternions

我正在绘制两个线框球体,我想跟随玩家。然而,当玩家移动两个小玩意时,当我旋转时,只有一个小玩意旋转。

破碎的Gizmo代码如下所示,它的偏移量应为45:

void OnDrawGizmosSelected() {
  Gizmos.color = new Color(1, 0, 0);

  Gizmos.matrix = Matrix4x4.TRS(transform.position, Quaternion.Euler(transform.rotation.x, transform.rotation.y + 45, transform.rotation.z), Vector3.one);
  Gizmos.DrawWireSphere(Vector3.zero, 5f);
}

这里的参考是整个块与两个小玩意:

void OnDrawGizmosSelected() {
  Gizmos.color = new Color(1, 0, 0);
  // This one works
  Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
  Gizmos.DrawWireSphere(Vector3.zero, 5f);

  // This one does not work
  Gizmos.matrix = Matrix4x4.TRS(transform.position, Quaternion.Euler(transform.rotation.x, transform.rotation.y + 45, transform.rotation.z), Vector3.one);
  Gizmos.DrawWireSphere(Vector3.zero, 5f);
}

默认没有旋转(旋转时我希望它如何停留)

Default Rotation

绕Y轴旋转

Rotation on Y Axis

1 个答案:

答案 0 :(得分:2)

Quaternion有4个组成部分,x,y,z和w。

将x,y和z放入Quaternion.Euler只会产生预期效果。

相反,请使用transform.rotation.eulerAngles

void OnDrawGizmosSelected()
{
    Gizmos.color = new Color(1, 0, 0);
    // This one works
    Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
    Gizmos.DrawWireSphere(Vector3.zero, 5f);

    // This one works now :)
    Gizmos.matrix = Matrix4x4.TRS(transform.position, Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 45, transform.rotation.eulerAngles.z), Vector3.one);
    Gizmos.DrawWireSphere(Vector3.zero, 5f);
}

修改

  

好的,它修复了Y值,但X和Z仍然被打破。他们移动但不是正确的方向。

然后尝试

    // This works even better
    Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one) * Matrix4x4.Rotate(Quaternion.Euler(0, 45, 0));