gpt4 book ai didi

c# - 统一: Rotate gizmo with an offset of 45

转载 作者:行者123 更新时间:2023-12-05 02:15:55 24 4
gpt4 key购买 nike

我正在绘制两个线框球体,我想跟随玩家四处走动。当玩家移动时,两个小玩意儿随之移动,但是,当我旋转时,只有一个小玩意儿旋转。

损坏的 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 的整个 block 的引用:

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

最佳答案

四元数 有 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));

关于c# - 统一: Rotate gizmo with an offset of 45,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50865791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com