簡體   English   中英

Unity3D C#在旋轉后計算正確的前進

[英]Unity3D C# calculate correct forward after rotation

我正在使用Unity開始我的開發。 我正在做那樣的事情:

    if(Input.GetKey(KeyCode.A))newValues[1]-=this.turnSpeed*(float)Time.deltaTime;
    if(Input.GetKey(KeyCode.D))newValues[1]+=this.turnSpeed*(float)Time.deltaTime;
    transform.Rotate(0, newValues[1], 0);


    if(Input.GetKey(KeyCode.W))newValues[0]+=this.speed*transform.forward.z*(float)Time.deltaTime;
    if(Input.GetKey(KeyCode.S))newValues[0]-=this.speed*transform.forward.z*(float)Time.deltaTime;


    transform.position = new Vector3(transform.position.x, transform.position.y, (float)newValues[0]);

所以我旋轉,我可以移動,但它只是在Z線移動,我知道我正在調用Z特定的運動。 但使用Javascript我可以做點什么

     transform.forward+=this.speed*transform.forward*(float)Time.deltaTime;

因此,我不需要執行新的矢量處理並復制到單獨的變量,它就像一個魅力,使用旋轉並在旋轉時使用自己作為方向。

你可能會誤解transform.forward的用法。 transform.forward只是一個向量來告訴你gameObject面向的方向,它取決於transform.rotation。

如果要移動GameObject,請始終使用transform.position

 transform.position += this.speed * transform.forward * (float)Time.deltaTime;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM