2016-12-06 47 views
0

這裏是我的移動使用內置的虛擬搖桿一個2D自上而下的目標代碼:移動和旋轉2D對象使用虛擬搖桿

void Update() 
    { 
     Vector2 moveVec = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), 
      CrossPlatformInputManager.GetAxis("Vertical")); 


     Vector3 lookVec = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"), 
      CrossPlatformInputManager.GetAxis("Vertical"), 4000); 

     transform.rotation = Quaternion.LookRotation(lookVec, Vector3.back); 
     transform.Translate(moveVec * Time.deltaTime * speed); 
    } 

沒有旋轉代碼,運動是完美的,添加後旋轉代碼的運動都搞砸了,如果方向朝下,它就會向上移動。 但是輪換正是它應該如何。

回答

0

只需將transform.Translate(moveVec * Time.deltaTime * speed);行更改爲transform.Translate(moveVec * Time.deltaTime * speed, Space.World);:默認情況下,轉換是相對於轉換自己的空間(Space.Self)進行的。

+0

這很好,我不想討厭,但如果你可以更多地解釋它,以及何時不使用「Space.World」,我將非常感激。 – Abdou023

+0

@ Abdou023那麼這取決於情況......在這種情況下,'Transform.Translate' [文檔](https://docs.unity3d.com/ScriptReference/Transform.Translate.html)指出,翻譯將是在變換的局部空間中進行:這意味着翻譯將受到對象方向的影響。您可以在ParticleSystem組件中找到相同類型的行爲:如果讓我們說一個在場景周圍移動的火焰粒子生成器,則在使用本地空間時,火焰將保持直線狀態,並且在使用世界空間時將會彎曲。希望這可以幫助 :) – Kardux