Quaternions – Unity 3D (1)
It is useful explain something in order to understand it. I am learning Unity and quite soon I found the necessity to digest Quaternions. At the moment I am still in the process to figure out how to deal with. Nevertheless I found some interesting things about Quaternions which I will expose in this post. I hope it will helps others to understand this data type.
Quaternions performs the 3 rotations in X, Y, Z without to use 3 different axis for each rotation. Therefore quaternions do not have the Gimbal lock issue, which happens when two rotation axis share the same plane. Quaternions deal with the 3 axis at the same time keeping the 3 axis always perpendicular to each other.
Quaternions represents the rotation in only one object. To work with, Quaternions have 4 parameters: X, Y, X, W. I not good idea modify this values, if your maths are not good enough. Unity provides the Class Quaternion and a series of methods to create rotations.
This line will set to 0 all axis rotation, where identity is a property from the class Quaternion. The second option (line 2) consists in create a default Quaternion with the same result.
transform.rotation = Quaternion.identity; transform.rotation = new Quaternion();
Euler Method represents the axis rotation related with the global system. The rotation is clockwise based and occurs first in Z then X and Y. The rotation is cumulative which means each rotation consider the previous rotation of the object.
// X, Y, Z transform.rotation = Quaternion.Euler(60, 30, 45);
The method LookRotation creates a rotation based in a vector where the Z axis is used as the forward direction and Y to Up. If the vector passed in the parameter is the position of an object, The rotation will be aligned to the vector created from the origin to the object (objective) position (line 1). If we subtract the 2 vector positions (object – objective), the object will points to the objective.
transform.rotation = Quaternion.LookRotation(objective.position); transform.rotation = Quaternion.LookRotation(transform.position - objective.position);