四元数外观旋转

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;