*Mathf
Unity Mathf 数学运算(C#)
*Vector3.Lerp
The second line uses Vector3.Lerp
to determine the zombie’s new location along the path between its current and target locations. Lerp
is a convenient method that interpolates between two values based on a third value.Lerp
clamps the third value between zero and one and interprets it as a fraction along the path between the two points. That means a value of 0
would return currentPosition
, a value of 1
would return target
, and a value of0.5
would return the midpoint between them.
Vector3.Lerp 插值
static function Lerp (from : Vector3, to : Vector3, t : float) : Vector3
Description描述
Linearly interpolates between two vectors.
两个向量之间的线性插值。
Interpolates from towards to by amount t.
按照数字t在from到to之间插值。
t is clamped between [0...1]. When t = 0 returns from. When t = 1 returns to. When t = 0.5 returns the average of from and to.
t是夹在 [0...1]之间,当t = 0时,返回from,当t = 1时,返回to。当t = 0.5 返回from和to的平均数。
// Animates the position to move from start to end within one second
//在1秒时间动画位置移动从from开始到to结束。
var start : Transform;
var end : Transform;
function Update () {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
另一个例子:
// Follows the target position like with a spring
//像弹簧一样跟随目标物体
var target : Transform;
var smooth = 5.0;
function Update () {
transform.position = Vector3.Lerp (
transform.position, target.position,
Time.deltaTime * smooth);
}
//---------------------------
You’ll use turnSpeed
in your calculations to control how quickly the zombie reorients himself to a new direction.
Unity uses quaternions internally to represent rotations. If you’re curious about the details of the math, check outthis info. Then, after you’ve cleaned up the mess from your head exploding, take solace in the fact that you really don’t need to know anything about quaternions when working with 2D.
That’s because the Quaternion.Euler
method lets you create a Quaternion
object from an Euler angle. Euler angles are the ones most people are accustomed to, consisting of individual x, y and z rotations. While they aren’t ideal for 3D work because of problems like gimbal lock, Euler angles are just fine for 2D games where you probably only want to rotate around the z-axis.
//---------------------------
Quaternion 四元数。
所以使用四元数
四元素的优势
十分易于差值(interpolate)
完全消除Gimbal Lock (或者说没有奇点)
尽管最后会把四元素还原为矩阵,但是四元素在连乘的时候效率大大优于矩阵
http://blog.csdn.net/wangxiaojun911/article/details/4644243
详细介绍Untiy3d里面的 http://www.cnblogs.com/softimagewht/archive/2010/11/16/1878331.html
//---------------------------
Gimbal lock
有关万向节死锁(Gimbal Lock)的问题 http://game.ceeger.com/Unity/Doc/2011/Gimbal_Lock.html
可以找找youtube的视频看一下。
欧拉角的万向节死锁就是这样:我们依次绕物体坐标系的X轴、Y轴、Z轴旋转,当Y轴旋转了90度之后,Z就会指向原来的X轴。这样一来,我们事实上只绕了X轴和Y轴两个轴旋转,第三根轴的自由度就丢失了!(值得指出的是,我们在描述的时候使用的是世界坐标系的x, y, z轴,但是旋转的时候却是使用绕物体的)