四元數外觀旋轉

Quaternion.LookRotation(Vector3 forward [, Vector3 up]) 將建立一個四元數旋轉,向前看向前向下並使 Y 軸與向上向量對齊。如果未指定向上向量,則將使用 Vector3.up。

旋轉此遊戲物件以檢視目標遊戲物件

// 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;