# unity旋转注视以及四元数的学习

Unity3D 一行代码实现一个物体保持面向另一个物体（旋转）

myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

myTransform面向target。

----------------------------------------一下转载一篇关于Quaternion的研究。

http://baike.baidu.com/view/319754.htm

function Rotate (eulerAngles : Vector3, relativeTo : Space = Space.Self) : void

Quaternion的变量比较少也没什么可说的，大家一看都明白。唯一要说的就是x\y\z\w的取值范围是[-1,1],物体并不是旋转一周就所有数值回归初始值，而是两周。

1）function ToAngleAxis (out angle : float, out axis : Vector3) : void
Description
Converts a rotation to angle-axis representation

var a=0.0;
var b=Vector3.zero;
transform.rotation.ToAngleAxis(a,b);

2）function SetFromToRotation (fromDirection : Vector3, toDirection : Vector3) : void
Description
Creates a rotation which rotates from fromDirection to toDirection.

var a:Vector3;
var b:Vector3;
var q:Quaternion;
var headUpDir:Vector3;

q.SetFromToRotation(a,b);
transform.rotation=q;
headUpDir=transform.TransformDirection(Vector3.Forward);

3）function SetLookRotation (view : Vector3, up : Vector3 = Vector3.up) : void
Description
Creates a rotation that looks along forward with the the head upwards along upwards
Logs an error if the forward direction is zero.

var obj1: Transform;
var obj2: Transform;
var q:Quaternion;

q.SetLookRotation(obj1.position, obj2.position);
transform.rotation=q;

4）function ToString () : string
Description
Returns a nicely formatted string of the Quaternion

Class Functions
1）四元数乘法 *

2) == 和 !=

3）static function Dot (a : Quaternion, b : Quaternion) : float
Description
The dot product between two rotations

4）static function AngleAxis (angle : float, axis : Vector3) : Quaternion
Description
Creates a rotation which rotates angle degrees around axis.

var obj1: Transform;
var obj2: Transform;
var q:Quaternion;

//物体沿obj2的z轴旋转，角度等于obj1的z轴。
q=Quaternion.AngleAxis(obj1.localEularAngle.z, obj2.TransformDirection(Vector3.forward));
transform.rotation=q;

5）static function FromToRotation (fromDirection : Vector3, toDirection : Vector3) : Quaternion
Description
Creates a rotation which rotates from fromDirection to toDirection.
Usually you use this to rotate a transform so that one of its axes eg. the y-axis - follows a target direction toDirection in world space.

6）static function LookRotation (forward : Vector3, upwards : Vector3 = Vector3.up) : Quaternion
Description
Creates a rotation that looks along forward with the the head upwards along upwards
Logs an error if the forward direction is zero.

7）static function Slerp (from : Quaternion, to : Quaternion, t : float) : Quaternion
Description
Spherically interpolates from towards to by t.

var obj1: Transform;
var t=0.1;
var q:Quaternion;

//让物体旋转到与obj1相同的方向
q=Quaternion.Slerp(transform.rotation, obj1.rotation,t);
transform.rotation=q;

q=Quaternion.Slerp(a,b,t);

q,a,b∈Quaternion

t[0,1]

q=a+(b-a)*t

var obj1: Transform;
var obj2：Transform；
var t=0.1;
var q:Quaternion;

//让物体obj1和obj2 朝向不同的方向，然后改变t
q=Quaternion.Slerp(obj1.rotation, obj2.rotation,t);
transform.rotation=q;

t+=Input.GetAxis("horizontal")*0.1*Time.deltaTime；

7）static function Lerp (a : Quaternion, b : Quaternion, t : float) : Quaternion
Description
Interpolates from towards to by t and normalizes the result afterwards.
This is faster than Slerp but looks worse if the rotations are far apart

8）static function Inverse (rotation : Quaternion) : Quaternion
Description
Returns the Inverse of rotation.

9）static function Angle (a : Quaternion, b : Quaternion) : float
Description
Returns the angle in degrees between two rotations a and b.

10）static function Euler (x : float, y : float, z : float) : Quaternion
Description
Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order).

Slerp 函数是非常常用的一个函数，用来产生旋转。
static function Slerp (from : Quaternion, to : Quaternion, t : float) : Quaternion

var obj1: Transform;
var obj2：Transform；
var speed:float;
var t=0.1;
var q:Quaternion;

q=Quaternion.Slerp(obj1.rotation, obj2.rotation,t);
transform.rotation=q;
t+=Time.deltaTime;

var target: Transform;
var rotateSpeed=30.0;
var t=float;
var q:Quaternion;

var wantedRotation=Quaternion.FromToRotation(transform.position,target.position);
t=rotateSpeed/Quaternion.Angle(transform.rotation,wantedRotation)*Time.deltaTime;
q=Quaternion.Slerp(transform.rotation, target.rotation,t);
transform.rotation=q;

• 本文已收录于以下专栏：

举报原因： 您举报文章：unity旋转注视以及四元数的学习 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)