1 回答

TA貢獻1877條經驗 獲得超6個贊
以下腳本將旋轉它附加到的游戲對象,以便將游戲對象保持在Target屏幕的中心,并使相機看起來與目標的方向相同。
public Transform Target;
public float Speed = 1f;
public Vector3 Offset;
void LateUpdate()
{
// Compute the position the object will reach
Vector3 desiredPosition = Target.rotation * (Target.position + Offset);
// Compute the direction the object will look at
Vector3 desiredDirection = Vector3.Project( Target.forward, (Target.position - desiredPosition).normalized );
// Rotate the object
transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.LookRotation( desiredDirection ), Time.deltaTime * Speed );
// Place the object to "compensate" the rotation
transform.position = Target.position - transform.forward * Offset.magnitude;
}
注意:永遠,永遠,永遠不要操縱四元數的組件,除非你真的知道你在做什么。四元數不存儲您在檢查器中看到的值。它們是用于表示旋轉的復雜實體,具有 4 個值。
- 1 回答
- 0 關注
- 218 瀏覽
添加回答
舉報