In Unity, Quaternion is used to represent rotation, which is a struct
, contains 4 components, x, y, z and w, representing a gameobject's coordinator in the scene. We can use Quaternion to do some common actions such as
Here's an example of how you might use a Quaternion in Unity:
using UnityEngine;
public class Example : MonoBehaviour
{
public float rotationSpeed = 10.0f;
private Quaternion targetRotation;
void Start()
{
targetRotation = Quaternion.Euler(0, 90, 0);
}
void Update()
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
}
In this example, we use the Quaternion.Euler
method to set the target rotation to a rotation of 90 degrees around the y-axis. In each Update
call, we use the Quaternion.Slerp
method to interpolate the rotation of the object transform
from its current rotation to the target rotation over time. The interpolation is controlled by the rotationSpeed
variable, which determines the speed at which the rotation occurs, and the Time.deltaTime
value, which gives us the time elapsed since the last Update
call.