Unity3D说明文档翻译-Rotation and Orientation in Unity

Rotation and Orientation in Unity

Unity内的旋转和方向

Summary

概要

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.

旋转在3D程序内通常表现为两种方式,四元数和欧拉角.各自有自己的优缺点.Unity内部使用四元数,但在检视面板上显示相等的欧拉角使你更容易编辑.

The Difference Between Euler Angles and Quaternions

欧拉角和四元数间的不同点

Euler Angles

欧拉角

Euler angles have a simpler representation, that being three angle values for X, Y and Z that are applied sequentially. To apply a Euler rotation to a particular object, each rotation value is applied in turn, as a rotation around its corresponding axis.

欧拉角有可简单的表示为3个角度值X,Y,Z.应用欧拉角旋转一个特定对象,每个旋转值按顺序应用为绕相应轴旋转.

Benefit: Euler angles have an intuitive “human readable” format, consisting of three angles.

Benefit: Euler angles can represent the rotation from one orientation to another through a turn of more than 180 degrees

Limitation: Euler angles suffer from Gimbal Lock. When applying the three rotations in turn, it is possible for the first or second rotation to result in the third axis pointing in the same direction as one of the previous axes. This means a “degree of freedom” has been lost, because the third rotation value cannot be applied around a unique axis.

好处:欧拉角有一个直观的”可读”格式,3个角组成.

好处:欧拉角可通过一个超过180度的转动表示旋转从一个方向转向另一个.

限制:欧拉角允许万向锁.当依次应用三个旋转时,很可能第一和第二个旋转导致第三个轴指向和之前一个轴相同的方向.这意味着”自由度”已经消失,因为第3个旋转值不能应用到一个唯一的轴.

Quaternions

四元数

Quaternions can be used to represent the orientation or rotation of an object. 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 will never normally need to know or modify the x, y & z properties.

四元数可用于表示一个对象的方向和旋转.它在内部表示为由4个数字组成(引用Unity内的X,Y,Z和w),然而这些数字不代表角度或轴,你通常不需要直接访问它们.除非你对四元数的数学运算特别感兴趣,你只需知道一个四元数表示一个3D空间内的旋转,而且你通常不需知道或修改X,Y和Z的属性.

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 because the rotation is measured in this way - from one orientation to another - that a quaternion can’t represent a rotation beyond 180 degrees.

与此相同,一个向量可表示一个位置或一个方向(方向是从原点开始测量的),一个四元数可表示一个方向或旋转,旋转从”原始”或”特定”的旋转开始测量的.因为旋转以这种方式测量-从一个方向到另一个-一个四元数不能表示一个超过180度的旋转.

Benefit: Quaternion rotations do not suffer from Gimbal Lock.

Limitation: A single quaternion cannot represent a rotation exceeding 180 degrees in any direction.

Limitation: The numeric representation of a Quaternion is not intuitively understandable.

优点:四元数旋转不允许万向锁.

限制:单个四元数不能表示一个在任何方向超过180度的旋转.

限制:四元数的数学表示不能直观的理解.

 

In Unity all Game Object rotations are stored internally as Quaternions, because the benefits outweigh the limitations.

Unity内所有对象旋转在内部都存储为四元数,因为优点大于限制.

In the Transform Inspector however, we display the rotation using Euler angles, because this is more easily understood and edited. New values entered into the inspector for the rotation of a Game Object are converted “under the hood” into a new Quaternion rotation value for the object.

在转换组件检视面板,我们使用欧拉角显示旋转,因为这更容易明白和编辑.键入到游戏对象在检视面板旋转上的新值将在后台转换为一个新的四元数旋转值.

The rotation of a Game Object 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 Game Object’s rotation. This is a value that is not possible to represent as a quaternion, so when you hit Play you’ll see that the object’s rotation values change to X: 0, Y: 5, Z: 0 (or thereabouts). This is because the rotation was converted to a Quaternion which does not have the concept of “A full 360-degree rotation plus 5 degrees”, and instead has simply been set to be oriented in the same way as the result of the rotation.

有一个副作用,可能在检视面板上键入一个为(0,365,0)的值作为一个游戏对象的旋转.这里有一个值不能以四元数表示,所以当你点击播放你将看到对象的旋转值变为(0,5,0)(或大约是).这是因为旋转被转换为四元数,它没有”一个完整的360度加5度”的概念,相反用相同方式设置调整旋转的结果.

Implications for Scripting

对脚本的影响

When dealing with handling rotations in your scripts, you should use the Quaternion class and its functions to create and modify rotational values. There are some situations where it is valid to use Euler angles, but you should bear in mind: - You should use the Quaternion Class functions that deal with Euler angles - Retrieving, modifying, and re-applying Euler values from a rotation can cause unintentional side-effects.

 当在你的脚本里处理旋转时,你需要使用四元数类和它的函数来创建和改变旋转值.有些时候适合使用欧拉角,但你要记住-你应该四元数类函数处理欧拉角-从一个旋转检索,修改和重复应用欧拉角可能会导致意外的副作用.

Creating and Manipulating Quaternions Directly

直接创建和操纵四元数

Unity’s Quaternion class has a number of functions which allow you to create and manipulate rotations without needing to use Euler angles at all. For example:

Unity的四元数类有许多函数允许你创建和操纵旋转不需要使用欧拉角.例如:

Creating:创建

Quaternion.LookRotation

Quaternion.AngleAxis

Quaternion.FromToRotation

Manipulating:操纵

Quaternion.Slerp

Quaternion.Inverse

Quaternion.RotateTowards

Transform.Rotate & Transform.RotateAround

However sometimes it’s desirable to use Euler angles in your scripts. In this case it’s important to note that you must keep your angles in variables, and only use them to apply them as Euler angles to your rotation. While it’s possible to retrieve Euler angles from a quaternion, if you retrieve, modify and re-apply, problems will arise.

然而有时在你的脚本里使用也是令人满意的.在这种情况下必须注意你必须保持你的角为变量,并只是用它们来应用于欧拉角旋转.虽然可能从一个四元数检索欧拉角,但如果检视,修改和重新应用,将会出现问题.

Here are some examples of mistakes commonly made using a hypothetical example of trying to rotate an object around the X axis at 10 degrees per second. This is what you should avoid:

这是一些错误的常见例子,使用一个假定的例子试图以每秒10度旋转一个对象的x轴.这是你应该避免的:

    // rotation scripting mistake #1

    // the mistake here is that we are modifying the x value of a quaternion

// this value does not represent an angle, and will not produce desired results

/*

旋转脚本错误1

这里的错误是我们在改变四元数的X,此值不是用角表示的,不会产生想要的结果

*/

    

    void Update () {

    

        var rot = transform.rotation;

        rot.x += Time.deltaTime * 10;

        transform.rotation = rot;

        

    }

    // rotation scripting mistake #2

    // the mistake here is that we are reading, modifying then writing the Euler

    // values from a quaternion. Because these values calculated from a Quaternion,

    // each new rotation may return very different Euler angles, which may suffer from gimbal lock.

/*

旋转脚本错误2

这里的错误是我们正从一个四元数读取,修改然后写入欧拉角值.因为这些值从一个四元数创建,每个新旋转将返回非常不同的允许万向锁的欧拉角.

*/    

    void Update () {

        

        var angles = transform.rotation.eulerAngles;

        angles.x += Time.deltaTime * 10;

        transform.rotation = Quaternion.Euler(angles);

 

    }

And here is an example of using Euler angles in script correctly:

这里是一个正确使用欧拉角的例子

    // rotation scripting with Euler angles correctly.

    // here we store our Euler angle in a class variable, and only use it to

    // apply it as a Euler angle, but we never rely on reading the Euler back.

 /*

正确使用欧拉角的旋转脚本

这里我们存储我们的欧拉角为一个类变量,并只把条作为欧拉角应用,但我们不依赖读取以前的欧拉角.

*/     

    float x;

    void Update () {

        

        x += Time.deltaTime * 10;

        transform.rotation = Quaternion.Euler(x,0,0);

 

    }

Implications for Animation

对动画的影响

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

许多3D创作包和Unity自己的内部动画窗口允许你在动画内使用欧拉角指定旋转.

These rotations values can frequently exceed range expressable by quaternions. For example, if an object should rotate 720 degrees in-place, this could be represented by Euler angles X: 0, Y: 720, Z:0. But this is simply not representable by a Quaternion value.

这些旋转值可能会频繁的超出四元数表示的范围.例如,如果一个对象需要原地旋转720度,这可用欧拉角表示为(0,720,0).但这不能同一个四元数值简单表示.

Unity’s Animation Window

Unity的动画窗口

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 will use Quaternion interpolation and rotate across the shortest distance to get there. See Using Animation Curves for more information on this.

Unity自己的动画窗口内,有些选项允许你指定该如何添加旋转-使用四元数或欧拉角添加.在指定欧拉角添加时你告诉Unity你要全范围指定角度.然而四元数旋转,你是说你仅仅希望旋转结束在一个特定方向上,而Unity将使用四元数添加和在最短距离内旋转.更多信息请参阅”使用动画曲线”.

External Animation Sources

外部动画资源

When importing animation from external sources, these files usually contain rotational keyframe animation in Euler format. Unity’s default behaviour is to resample these animations and generate a new Quaternion keyframe for every frame in the animation, in an attempt to avoid any situations where the rotation between keyframes may exceed the Quaternion’s valid range.

当从外部资源导入动画,这些文件通常包含了欧拉角格式的旋转关键帧动画.Unity的默认行为是对这些动画重新取样并在动画内给每个帧产生一个新的四元数关键帧,试图避免任何关键帧之间的旋转会超出四元数允许范围的情况.

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 will work correctly.

例如,设想两个关键帧,相距6帧,第一个关键帧的x轴的值为0而第二个关键帧的为270.没有重新采样,一个四元数添加到这两个关键帧之间将在相反方向旋转90度,因为这是从第一个方向到第二个方向最短的方式.然而使用重新取样并添加一个关键帧到每一帧,则只有45度在两个关键帧之间,所以旋转将正确工作.

There are still some situations where - even with resampling - the quaternion representation of the imported animation may not match the original closely enough, For this reason, in Unity 5.3 and onwards there is the option to turn off animation resampling, so that you can instead use the original Euler animation keyframes at runtime. For more information, see Animation Import of Euler Curve Rotations.

依然有一些情况-即使重新取样-用四元数表示导入动画也将不会充分紧密的匹配原始的,为此,在Unity5.3之后有关闭动画重新采样的选项,这样你可以实时替换使用原来的欧拉角动画关键帧.更多信息参见”动画的欧拉角曲线旋转导入”.

 

### 回答1: Odin Inspector是一个操作Unity对象的插件,可以增强Unity编辑器中的开发体验。Odin Inspector 3.0.4版本是该插件的一个更新版本,可以通过CSDN下载获得。 该插件通过注解的方式可以为Unity对象添加更多的编辑器控件,如:枚举选择器,拖拽选择器,开关按钮等。同时,该插件也提供了一些常用的数据类型,如:字典、序列化树等。 Odin Inspector使用起来非常方便,只需要在需要增强的类或属性前添加对应的注解即可,这样就可以在Unity编辑器中变得更加灵活和直观。此外,Odin Inspector还提供了一些自定义的标记属性,可用于美化编辑器界面或增加辅助功能。 总的来说,Odin Inspector插件是一个非常实用的Unity开发工具,可以为Unity开发者提高开发效率和开发体验。而CSDN下载的unity3d文档类资源,也为我们提供了方便的使用说明和示例代码,有助于我们更快地上手和运用该插件。 ### 回答2: Odin Inspector是Unity3D的插件,这个插件可以在Unity编辑器中为开发人员提供更多的调试、编辑和自定义选项。Odin Inspector的3.0.4版本是最新的版本,这个版本通过提供更多的功能来提高了编辑器的性能和效率。Odin Inspector 3.0.4支持C#生成代码,这使得开发者可以以更快的速度编写代码,而且还提供了一个强大的反射系统,能够在使用过程中自动为脚本创建编辑器。此外,Odin Inspector可以让开发者轻松地创建自定义的编辑器窗口和面板,并提供多种工具和快捷键以帮助开发者更加高效地开发Unity项目。 而在CSDN上可以下载Unity3D文档类资源,这些资源包括了Unity3D的官方文档以及其他一些非官方的教程和手册。这些资源可以帮助开发者学习Unity3D的基础知识和高级技巧,并且对于使用Odin Inspector来增强开发人员的编辑器体验来说也非常有用。在CSDN上下载这些资源非常容易,只需要注册并登录即可随时下载和使用。 总之,Odin Inspector版本3.0.4和CSDN中的Unity3D文档类资源都可以帮助Unity3D开发者提高效率和编写更好的代码。它们是Unity3D开发过程中的重要工具,能够帮助开发者更好地利用Unity编辑器的功能,并加速开发过程。 ### 回答3: Unity是一款流行的游戏引擎,为了方便开发者使用Unity,社区推出了许多插件,其中Odin Inspector 3.0.4版本是一个非常优秀的插件。这个插件可以用来简化Unity的开发流程,提高开发效率。 Odin Inspector 3.0.4版本主要提供了一些高级编辑器功能,如可定制化的Inspector面板、可扩展属性系统、强大的序列化和反序列化等。它还包含了一些常用的扩展功能,如资源管理器、GUI工具、命名空间工具等等。它的强大功能让开发者可以更加方便地设置和管理游戏中的各种对象和组件。 此外,Unity3D文档类资源在开发过程中也起到了不可替代的作用。这些文档提供了关于Unity3D的详细信息和指南,包括游戏开发、图形渲染、物理引擎、脚本编程等方面的内容。 Odin Inspector 3.0.4版本和Unity3D文档类资源可以通过CSDN进行下载,CSDN是一个技术社区,拥有大量的技术资源和开发者社区。通过下载Odin Inspector 3.0.4版本和Unity3D文档类资源,开发者可以更加高效地使用Unity进行游戏开发,同时也可以获得更多的技术帮助和指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值