unity3d Quaternion Look Rotation


Quaternion.LookRotation(Vector3 forward [, Vector3 up]) will create a Quaternion rotation that looks forward 'down' the forward vector and has the Y axis aligned with the 'up' vector. If the up vector is not specified, Vector3.up will be used.

Rotate this Game Object to look at a target Game Object

// Find a game object in the scene named Target
public Transform target = GameObject.Find("Target").GetComponent<Transform>();

// We subtract our position from the target position to create a
// Vector that points from our position to the target position
// If we reverse the order, our rotation would be 180 degrees off.
Vector3 lookVector = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(lookVector);
transform.rotation = rotation;