Unity:以 45 的偏移量旋转 gizmo
Unity: Rotate gizmo with an offset of 45
我正在绘制两个线框球体,我想跟随玩家四处走动。当玩家移动时,两个小工具会随之移动,但是,当我旋转时,只有一个小工具会旋转。
损坏的 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);
}
这里是包含两个 gizmo 的整个方块供参考:
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);
}
默认不旋转(我希望它在旋转时保持不动)
绕 Y 轴旋转
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);
}
编辑:
Okay, that fixes the Y value, but X and Z are still broken. They move but not in the proper direction.
那就试试
// This works even better
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one) * Matrix4x4.Rotate(Quaternion.Euler(0, 45, 0));
我正在绘制两个线框球体,我想跟随玩家四处走动。当玩家移动时,两个小工具会随之移动,但是,当我旋转时,只有一个小工具会旋转。
损坏的 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);
}
这里是包含两个 gizmo 的整个方块供参考:
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);
}
默认不旋转(我希望它在旋转时保持不动)
绕 Y 轴旋转
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);
}
编辑:
Okay, that fixes the Y value, but X and Z are still broken. They move but not in the proper direction.
那就试试
// This works even better
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one) * Matrix4x4.Rotate(Quaternion.Euler(0, 45, 0));