RotateTowards
Rotates a rotation from towards to.
Read time 2 minutesLast updated 10 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
RotateTowards(Quaternion, Quaternion, float)
Rotates a rotation towards .
fromtopublic static Quaternion RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta)
Parameters
The unit quaternion to be aligned with .
toThe target unit quaternion.
The maximum angle in degrees allowed for this rotation.
Returns
Type | Description |
|---|---|
| Quaternion | A unit quaternion rotated towards |
Remarks
The quaternion is rotated towards by an angular step of . The rotation will not overshoot the quaternion. Negative values of moves away from until the rotation is exactly the opposite direction.
fromtomaxDegreesDeltatomaxDegreesDeltatoExamples
using UnityEngine;public class Example : MonoBehaviour{ // The object whose rotation we want to match. public Transform target; // Angular speed in degrees per sec. public float speed = 1.0f; void Update() { // The step size is equal to speed times frame time. var step = speed * Time.deltaTime; // Rotate our transform a step closer to the target's. transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, step); }}
RotateTowards(Quaternion, Quaternion, float)
Rotates a rotation towards .
fromtopublic static Quaternion RotateTowards(in Quaternion from, in Quaternion to, float maxDegreesDelta)
Parameters
The unit quaternion to be aligned with .
toThe target unit quaternion.
The maximum angle in degrees allowed for this rotation.
Returns
Type | Description |
|---|---|
| Quaternion | A unit quaternion rotated towards |
Remarks
The quaternion is rotated towards by an angular step of . The rotation will not overshoot the quaternion. Negative values of moves away from until the rotation is exactly the opposite direction.
fromtomaxDegreesDeltatomaxDegreesDeltatoExamples
using UnityEngine;public class Example : MonoBehaviour{ // The object whose rotation we want to match. public Transform target; // Angular speed in degrees per sec. public float speed = 1.0f; void Update() { // The step size is equal to speed times frame time. var step = speed * Time.deltaTime; // Rotate our transform a step closer to the target's. transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, step); }}