turn from: http://www.cnblogs.com/tgycoder/p/5103966.html
First, Introduction
Quaternion, also known as the four-dollar number, consists of four components of X,y,z and W, a mathematical concept discovered by the Irish mathematician William Lu Yun Hamilton in 1843. The multiplication of the four-dollar number does not conform to the Exchange law. From a clear point of view, the four-dollar number is an irreducible extension of the plural. If the set of the four-number is considered as a multi-dimensional real space, the four-dollar number represents a four-dimensional space, which is relative to the plural as a two-dimensional space.
four Yuan Count
About the nature of the four-yuan number, and the relationship between rotation, ball-type linear interpolation, please read the 3D game and computer graphics in the mathematical method-four yuan, not much to do this introduction. The following main introduction is unity in the four Yuan number-quaternion.
In unity, the quaternion is used to store and represent the angle of rotation of an object. The transformation of quaternion is very complicated, and it can be realized by the related methods in transform for gameobject general rotation and movement. second, quaternion class attributes eulerangles-Euler pt .
Defined
Public Vector3 Eulerangles{get;set;}
How to change the rotation state of a game object, we can change its transform to the order of Euler angle transformation, for example, suppose P (x,y,z) is a point on a gameobject, rotates a corner around the x-axis, rotates b in the Y axis, rotates the c angle around the z-axis, and then rotates the state p ' ( X ', y ', Z '). Unity of the implementation process is very simple, a code can be done. However, the specific implementation process is indeed very complex, please read the 3D game and computer graphics in the mathematical method-transform.
Here's an example of how to use Euler angles.
Using Unityengine;
Using System.Collections;
public class Eulerangler_ts:monobehaviour {public
Transform A, B;
quaternion rotations = quaternion.identity;
Vector3 eulerangle = Vector3.zero;
float speed = 10.0f;
float tspeed = 0.0f;
Use this for initialization
void Start () {
}
//Update are called once per frame
void Update () {
tspeed + = speed * Time.deltatime;
The first way: Assign the quaternion instance object to the transform rotation
rotations.eulerangles = new Vector3 (0.0f, Tspeed, 0.0f);
A.rotation = rotations;
The second way: the Euler angle represented by the three-bit vector is directly assigned to the transform eulerangle
b.eulerangles = new Vector3 (0.0f, Tspeed, 0.0f);
}
three, Quaternion class instance method
1, Setfromtorotation Method-Create rotation instance
1.1 Function Prototypes
public void Setfromtorotion (Vector3 fromdirection,vector3 todirection);
You can create a quaternion instance from formdirection to Todirection.
quaternion q = quaternion.identity;
Q.setfromtorotation (V1,V2);
transform.rotation = q;
You can transform the Gameobject object by first gameobject the x,y,z axis direction of the object's own coordinate system and the x,y,z axis of the world coordinate system, and then rotate the vector gameobject direction in the V1 object's own coordinate system to the V2 direction. 1.2 PS: cannot be set directly using Transform.rotation.SetFromToRotation (V1,V2), only the instantiated quaternion can be copied to transform.rotation. 1.3 Example Demo
Using Unityengine;
Using System.Collections;
public class Setfromtodirection_ts:monobehaviour {public
Transform A, B, C;
quaternion q = quaternion.identity;
Use this to initialization
void Start () {
}
//Update is called once per frame
void Update () {
q . Setfromtorotation (A.position, b.position);
c.rotation = q;
Debug.drawline (Vector3.zero, a.position, color.red);
Debug.drawline (Vector3.zero, b.position, color.green);
Debug.drawline (c.position, c.position + new Vector3 (0.0f, 1.0f, 0.0f), color.black);
Debug.drawline (C.position, C.transformpoint (Vector3.up * 1.5f), color.yellow);
}
The results of the operation are shown in the following illustration:
2, Setlookrotation Method-Set the quaternion instance orientation 2.1 Function Prototypes
public void Setlookrotation (Vector3 view);
public void Setlookrotation (Vector3 view,vector3 up);
For example:
quaternion q = quaternion.identity;
Q.setlookrotation (V1,V2);
transform.rotation = q;
The Transform.forward direction is the same as the V1 direction.
The transform.right is perpendicular to the plane formed by the 3 points of Vector3.zer0, V1 and V2.
The V2 determines the orientation of the transform.up, because when the Transform.forward and transform.right direction are determined, the direction of the transform.up will always be less than or equal to 90 degrees in the direction of the V2.
When V1 is Vector3.zero, the method fails. 2.2 PS: Ibid., do not instantiate quaternion objects directly using the Transform.rotation.SetLookRotation (V1,v2) method. 2.3 Example Demo
Using Unityengine;
Using System.Collections;
public class Setlookrotation_ts:monobehaviour {public
Transform A, B, C;
quaternion q = quaternion.identity;
Use this to initialization
void Start ()
{
}
//Update is called once per frame
void Update ()
{
q.setlookrotation (a.position, b.position);
c.rotation = q;
Debug.drawline (Vector3.zero, a.position, color.red);
Debug.drawline (Vector3.zero, b.position, color.green);
Debug.drawline (C.position, C.transformpoint (Vector3.right * 1.5f), color.black);
Debug.drawline (C.position, C.transformpoint (Vector3.forward * 1.5f), color.yellow);
Debug.Log ("C.right with a": + Vector3.angle (c.right, a.position));
Debug.Log ("The angle of the C.right and B:" + vector3.angle (c.right, b.position));
Debug.Log (The angle between "c.up and B:" + vector3.angle (c.up, b.position));
}
Run results
3, Toangleaxis method 3.1 Function Prototypes
public void Toangleaxis (out float angle,out Vector3 axis);
Parameter angle is the rotation angle, and the parameter axis is an axis vector.
This function enables the transformation of the Gameobject object's rotation from the quaternion.identity state to the current state, simply by rotating the Gameobject object around the axis axis (world coordinate system) to the angle angle. 3.2 Example Demo
using unityengine; using System.Collections;
public class Toangleaxis_ts:monobehaviour {public Transform A, B;
float angle;
Vector3 axis = Vector3.zero;
float xspeed = 0.0f, Yspeed = 0.0f, zspeed = 0.0f; Use this to initialization void Start () {}//Update is called once per frame void Update
() {xspeed = 0.5f * time.deltatime;
Yspeed + + 1.0f * time.deltatime;
Zspeed + + 2.5f * time.deltatime;
A.eulerangles = new Vector3 (xspeed, Yspeed, zspeed);
Gets the rotation axis and angle a.rotation.toangleaxis (out angle, out axis) of a rotation;
The rotation of B is set so that the rotation and a of B are the same b.rotation = Quaternion.angleaxis (angle, axis); }
}