關於Unity3d的Quaternion.LookRotation的學習

首先,建立兩個cube GameObject物件。Origin做爲原點參考。Player是咱們要實驗的物件。以下圖所示:html

建立一個腳本Quat.cs,賦給Palyer cube。腳本以下:spa

public class Quat : MonoBehaviour
{
    /// <summary>
    /// 向前的方向
    /// </summary>
    public Vector3 Forward;
    /// <summary>
    /// 向上的方向
    /// </summary>
    public Vector3 Up;
    
    public Vector3 MoveV;

    private void Start()
    {
        Forward = Vector3.forward;
        Up = Vector3.zero;
        MoveV = Vector3.forward;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            transform.rotation = Quaternion.LookRotation(Forward,Up);
            transform.Translate(MoveV);

        }
    }

}

這時候時候運行,能夠看到每點擊一次,Player往前走1 米。若是咱們此時把上圖中右下角紅框中Forward的z的1改爲-1,咱們會發現它圍繞X軸旋轉了180度,每點擊一次,Player往之前的反方向走1米。pwa

經過修改上面的參數,不斷的實驗,咱們能夠發現Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up)中forward是指z軸的正數方向爲前進的方向仍是負數的方向爲前進的方向,無數值的大小。upwards 參數亦如此。3d

相關文章
相關標籤/搜索