Unity3d-EulerAngles(局部和全局)与检查器中的完全不同 [英] Unity3d - eulerAngles (local and global) totally different than what's in inspector

查看:85
本文介绍了Unity3d-EulerAngles(局部和全局)与检查器中的完全不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在游戏对象检查器中,我使用的起始旋转角度是"-90",但是当我运行print(transform.eulerAngles.x)时,我得到270(transform.localEulerAngles.x的同上).

In the inspector for a gameObject I'm using the starting rotation is "-90", but when I run print(transform.eulerAngles.x) I get 270 (ditto for transform.localEulerAngles.x).

如果我将gameObject向下倾斜,则检查器X值将变大(例如达到-85).打印的transform.eulerAngles.x也变大了,例如274.

If I tilt the gameObject downward, the inspector X value gets bigger (say, to -85) as it should. The printed transform.eulerAngles.x also gets bigger, say to 274.

这是很奇怪的地方:

如果我将gameObject向上倾斜,则检查器的x坐标会变小(例如,变为-95),但是打印的eulerAngle.x值会变大(此处为274).因此,如果我将eulerAngle.x的对象从270向上或向下旋转,则x值都会增加.

If I tilt the gameObject upward the inspector x coordinate gets smaller (ex, to -95), as it should, BUT the printed eulerAngle.x value gets BIGGER (here to 274). So if I rotate the object up or down from the eulerAngle.x being 270, the x value increases regardless.

我肯定在这里做错了事,但是经过大量的故障排除后,我仍然不知道该怎么办.有什么想法吗?

I'm definitely doing something wrong here, but after a lot of troubleshooting I still can't figure out what. Any thoughts?

推荐答案

eulerAngles是Unity3D中复杂的过程.您切勿在检查器中或通过代码增加/减少或设置值.应该只使用绝对值来读取和设置它.

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.

请勿增加或减少值,因为当角度超过360度时,它将失败.而是使用Transform.Rotate.

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

这是Unity3D文档示例中的示例代码:

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);
    }
}

这直接来自文档: https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html

也Transform.Rotate文档: https://docs.unity3d.com/ScriptReference/Transform.Rotate.html

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

与您登录时相比,检查器几乎总是会为您提供一个时髦的值.您将获得的唯一一致性是print(transform.rotation).这应该在检查器和代码之间保留相似的值.

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.

这篇关于Unity3d-EulerAngles(局部和全局)与检查器中的完全不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆