Version: 2020.2
Scale Constraints
Lights (luces)

Rotación y Orientación en Unity

Rotations in 3D applications are usually represented in one of two ways: Quaternions or Euler angles. Each has its own uses and drawbacks. Unity uses Quaternions internally, but shows values of the equivalent Euler angles in the Inspector to make it easy for you to edit.

Euler angles and quaternions

Euler angles

Euler angles are represented by three angle values for X, Y and Z that are applied sequentially. To apply an Euler rotation to a particular GameObject, each rotation value is applied in turn, as a rotation around its corresponding axis.

  • Beneficio: Los ángulos de Euler tienen un formato intuitivo de “lectura humana”, que consta de tres ángulos.
  • Beneficio: Los ángulos de Euler pueden representar la rotación de una orientación a otra a través de un giro de más de 180 grados
  • ** Limitación **: Los ángulos de Euler sufren de Gimbal Lock. Cuando se aplican las tres rotaciones a su vez, es posible que la primera o segunda rotación dé lugar al tercer eje que apunta en la misma dirección que uno de los ejes anteriores. Esto significa que se ha perdido un “grado de libertad”, porque el tercer valor de rotación no puede aplicarse alrededor de un eje único.

Quaternions

Quaternions can be used to represent the orientation or rotation of a GameObject. This representation internally consists of four numbers (referenced in Unity as x, y, z & w) however these numbers don’t represent angles or axes and you never normally need to access them directly. Unless you are particularly interested in delving into the mathematics of Quaternions, you only really need to know that a Quaternion represents a rotation in 3D space and you never normally need to know or modify the x, y & z properties.

In the same way that a Vector can represent either a position or a direction (where the direction is measured from the origin), a Quaternion can represent either an orientation or a rotation - where the rotation is measured from the rotational “origin” or “Identity”. It is because the rotation is measured in this way - from one orientation to another - that a quaternion can’t represent a rotation beyond 180 degrees.

  • Beneficio: Las rotaciones Quaternion no sufren de Gimbal Lock.
  • Limitación: Un único quaternion no puede representar una rotación que exceda 180 grados en cualquier dirección.
  • Limitación: La representación numérica de un Quaternion no es intuitivamente comprensible.

Unity stores all GameObject rotations internally as Quaternions, because the benefits outweigh the limitations.

The Transform Inspector displays the rotation using Euler angles, because this is easier to understand and edit. Unity converts new values into the Inspector for the rotation of a GameObject into a new Quaternion rotation value for the GameObject.

The rotation of a GameObject is displayed and edited as Euler angles in the Inspector, but is stored internally as a Quaternion
The rotation of a GameObject is displayed and edited as Euler angles in the Inspector, but is stored internally as a Quaternion

As a side-effect, it is possible in the Inspector to enter a value of, say, X: 0, Y: 365, Z: 0 for a GameObject’s rotation. This value is not possible to represent as a quaternion, so when you enter Play mode, the GameObject’s rotation values change to X: 0, Y: 5, Z: 0. This is because Unity converts rotation to a Quaternion which does not have the concept of a full 360-degree rotation plus 5 degrees, and instead is set to orient the same way as the result of the rotation.

To learn more about scripting with the Quaternion class, see the Quaternion User Manual page and the Quaternion Scripting Reference

Implications for Animation

Many 3D authoring packages, and Unity’s own internal Animation window, allow you to use Euler angles to specify rotations during an animation.

These rotations values can frequently exceed ranges expressable by quaternions. For example, if a GameObject rotates 720 degrees, this could be represented by Euler angles X: 0, Y: 720, Z:0. But this is not representable by a Quaternion value.

Unity’s Animation Window

Within Unity’s own animation window, there are options which allow you to specify how the rotation should be interpolated - using Quaternion or Euler interpolation. By specifying Euler interpolation you are telling Unity that you want the full range of motion specified by the angles. With Quaternion rotation however, you are saying you simply want the rotation to end at a particular orientation, and Unity uses Quaternion interpolation and rotate across the shortest distance to get there. See Using Animation Curves for more information on this.

External Animation Sources

Al importar animaciones de fuentes externas, estos archivos normalmente contienen animación de frames clave rotativos en formato Euler. El comportamiento predeterminado de Unity es volver a muestrear estas animaciones y generar un nuevo frame clave de Quaternion para cada frame de la animación, en un intento de evitar situaciones en las que la rotación entre fotogramas clave pueda exceder el rango válido de Quaternion.

For example, imagine two keyframes, 6 frames apart, with values for X as 0 on the first keyframe and 270 on the second keyframe. Without resampling, a quaternion interpolation between these two keyframes would rotate 90 degrees in the opposite direction, because that is the shortest way to get from the first orientation to the second orientation. However by resampling and adding a keyframe on every frame, there are now only 45 degrees between keyframes so the rotation works correctly.

Todavía hay algunas situaciones en las que - incluso con el remuestreo - la representación de quaternion de la animación importada puede no coincidir con el original lo suficientemente estrechamente, por eso, en Unity 5.3 y en adelante existe la opción de desactivar el resampling (remuestreo) de animación, Utilice los frames clave de la animación original de Euler en tiempo de ejecución. Para obtener más información, consulte Importación de animación de Rotaciones de Curvas Euler.

Scale Constraints
Lights (luces)
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961