Unity 中数学计算的困惑

为什么 Debug.Log 显示一个值,而 Unity 编辑器显示另一个值?

我试图了解计算,似乎非常清晰,但直到我注意到冲突的数字,似乎有一个我不知道的数学规则。

using UnityEngine;

public class testScript : MonoBehaviour
{
    [SerializeField] GameObject rightTurnRoad;
    [SerializeField] GameObject straighRoad;

    Transform currentExit;

    private void Start()
    {
        SpawnRoad();
    }

    private void SpawnRoad()
    {
        for (int i = 0; i < 4; i++)
        {
            GameObject road = Instantiate(rightTurnRoad);
            road.name = $"RoadTurn_{i}";

            Transform enter = road.transform.Find("EnterPoint");
            Transform exit = road.transform.Find("ExitPoint");

            Debug.Log($"\n================ ROAD {i} ================");

            Debug.Log($"ENTER LOCAL ROT: {enter.localRotation}");
            Debug.Log($"ENTER WORLD ROT: {enter.rotation}");
            Debug.Log($"ENTER LOCAL EULER: {enter.localEulerAngles}");
            Debug.Log($"ENTER WORLD EULER: {enter.eulerAngles}");
            Debug.Log($"ENTER WORLD POS: {enter.position}");
            Debug.Log($"ENTER RIGHT: {enter.right}");

            Debug.Log($"EXIT LOCAL ROT: {exit.localRotation}");
            Debug.Log($"EXIT WORLD ROT: {exit.rotation}");
            Debug.Log($"EXIT WORLD EULER: {exit.eulerAngles}");
            Debug.Log($"EXIT WORLD POS: {exit.position}");
            Debug.Log($"EXIT RIGHT: {exit.right}");

            if (currentExit == null)
            {
                Debug.Log("<color=yellow><b>FIRST ROAD</b></color>");

                road.transform.position = Vector3.zero;
                road.transform.rotation = Quaternion.identity;
            }
            else
            {
                Debug.Log("<color=#00FFFF><b>SNAP START</b></color>");

                Debug.Log($"CURRENT EXIT ROT: {currentExit.rotation}");
                Debug.Log($"CURRENT EXIT POS: {currentExit.position}");

                Debug.Log($"ROAD BEFORE ROT: {road.transform.rotation}");

                Quaternion rotationDifferent =
                    currentExit.rotation * Quaternion.Inverse(enter.rotation);

                Debug.Log($"ROT DIFF (QUAT): {rotationDifferent}");
                Debug.Log($"ROT DIFF (EULER): {rotationDifferent.eulerAngles}");

                road.transform.rotation =
                    rotationDifferent;

                Debug.Log($"ROAD AFTER ROT: {road.transform.rotation}");

                Debug.Log($"ENTER WORLD AFTER ROT: {enter.rotation}");

                Vector3 positionDifferent =
                    currentExit.position - enter.position;

                Debug.Log($"ENTER POS AFTER ROT: {enter.position}");
                Debug.Log($"POS DIFF: {positionDifferent}");

                road.transform.position += positionDifferent;

                Debug.Log($"ROAD FINAL POS: {road.transform.position}");
            }

            currentExit = exit;
        }
    }
}

我曾经问过 chatGPT 添加大量调试信息,因为我无法理解发生了什么或如何实现这一点。最终,我变得如此沮丧,以至于我只关闭了项目。

以下是情况:

我有一个道路预设体,内部有两个点,以便我可以创建另一个道路段并将其正确附加。

道路 ExitPoint 的所有问题都解决了。问题出现在 EnterPoint 坐标上。

例如,Enter 点的世界旋转是 180 度,但 Unity 始终将其转换为 0 度。在这种情况下,0 是技术上正确的。但是,为什么它会转换为 0,如果,无论我如何移动它、旋转它、删除它或进行任何其他操作,坐标仍然保持 180 度?

可能是道路本身的旋转和 Enter 点的旋转是被组合或从彼此中减去的。然而,我不知道 Unity 为何会这样做。

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Transform))]
public class TransformEditorDebug : Editor
{
    public override void OnInspectorGUI()
    {
        Transform t = (Transform)target;

        DrawDefaultInspector();

        EditorGUILayout.Space();

        EditorGUILayout.LabelField("WORLD POSITION", t.position.ToString());
        EditorGUILayout.LabelField("LOCAL POSITION", t.localPosition.ToString());

        EditorGUILayout.LabelField("WORLD EULER", t.eulerAngles.ToString());
        EditorGUILayout.LabelField("LOCAL EULER", t.localEulerAngles.ToString());
    }
}

这是我的(chatGPT)自定义检查器,可能会有所帮助。您可以忽略自定义脚本,因为我甚至从父项中分离了 Enter 点来证明一次又一次地,它的世界旋转不是 0。