LookRotation
Creates a rotation with the specified forward and upwards directions.
Read time 2 minutesLast updated 10 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
LookRotation(Vector3, Vector3)
Creates a rotation with the specified and directions.
forwardupwardspublic static Quaternion LookRotation(Vector3 forward, Vector3 upwards)
Parameters
Returns
Type | Description |
|---|---|
| Quaternion |
Remarks
Z axis will be aligned with , X axis aligned with cross product between and , and Y axis aligned with cross product between Z and X.
forwardforwardupwardsIf the vector is zero, the method logs an error in the console and returns .
forwardidentityIf and are colinear, or if the magnitude of is zero, the result is the same as Quaternion.FromToRotation with set to the positive Z-axis (0, 0, 1) and set to the normalized direction.
forwardupwardsupwardsfromDirectiontoDirectionforward// You can also use transform.LookAtusing UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ public Transform target; void Update() { Vector3 relativePos = target.position - transform.position; // the second argument, upwards, defaults to Vector3.up Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up); transform.rotation = rotation; }}
Additional Resources: Quaternion.SetLookRotation.
LookRotation(Vector3, Vector3)
Creates a rotation with the specified and directions.
forwardupwardspublic static Quaternion LookRotation(in Vector3 forward, in Vector3 upwards)
Parameters
Returns
Type | Description |
|---|---|
| Quaternion |
Remarks
Z axis will be aligned with , X axis aligned with cross product between and , and Y axis aligned with cross product between Z and X.
forwardforwardupwardsIf the vector is zero, the method logs an error in the console and returns .
forwardidentityIf and are colinear, or if the magnitude of is zero, the result is the same as Quaternion.FromToRotation with set to the positive Z-axis (0, 0, 1) and set to the normalized direction.
forwardupwardsupwardsfromDirectiontoDirectionforward// You can also use transform.LookAtusing UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ public Transform target; void Update() { Vector3 relativePos = target.position - transform.position; // the second argument, upwards, defaults to Vector3.up Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up); transform.rotation = rotation; }}
Additional Resources: Quaternion.SetLookRotation.