Unity3d - eulerAngles (local and global) totally different than what's in inspector

南楼画角 提交于 2019-12-01 20:59:29

eulerAngles are a convoluted process in Unity3D. You should never increment/decrement or set the values in your inspector or via code. Should only use absolute values to read and set it.

Don't increment or decrement the values, as it will fail when the angle exceeds 360 degrees. Transform.Rotate is what you use instead.

Here is the sample code in the Unity3D documentation example:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public float yRotation = 5.0F;
    void Update() {
        yRotation += Input.GetAxis("Horizontal");
        transform.eulerAngles = new Vector3(10, yRotation, 0);
    }
    void Example() {
        print(transform.eulerAngles.x);
        print(transform.eulerAngles.y);
        print(transform.eulerAngles.z);
    }
}

This is taken directly from the documentation: https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html

Also Transform.Rotate documentation: https://docs.unity3d.com/ScriptReference/Transform.Rotate.html

The inspector will almost always give you a funky value vs when you log it. The only consistency you will get is print(transform.rotation). This should retain similar values across inspector and code.

Nathan VanHouten

for each value in transform.eulerAngles or transform.localEulerAngles if it has a parent:-

if (value > 270)
    value =- 360;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!