2017-04-27 29 views
0

在我的遊戲中,我有一個pacman風格的舞臺。邊緣碰撞器放置在每條線上。 現在,對於我的播放器的移動(WASD樣式),我最初使用了transform.Translate。這給了我希望我的球員擁有的最佳流暢動作。你們大多數人都知道,這使得邊緣對撞機無效,因爲玩家只會將其位置轉移到邊緣。爲了解決這個問題,我使用了rb.AddForce(new Vector2(Input.GetAxis(「Horizo​​ntal」)* speed,Input.GetAxis(「Vertical」)* speed);相反,我真的不喜歡玩家如何移動,因爲即使你放開WSAD鍵,它仍然會移動,並且它會發出更多的加速移動而不是計算的移動。運動問題

有沒有人知道解決此問題的方法?流體WASD運動(如transform.Translate)不通過邊對撞機。

我很感激!

+0

參見[這](https://stackoverflow.com/questions/42537362/stop-character - 立即 - 當鍵被釋放/ 42537458#42537458)和[this](https://stackoverflow.com/questions/43166587/stop-object-instantly-after-collison/43167072#43167072)。它們都描述瞭如何在釋放鍵時停止對象。不,你不應該用'transform.Translate'移動剛體。 AddForce或移動功能是好的。 – Programmer

回答

0

你的對象還是移動的原因是留在剛體的速度。我的建議是,直接改變速度。另外,一定要把你的代碼放在Fixed中更新。

Vector2 inputVector = new Vector2(Input.GetAxis("Vertical", Input.GetAxis("Horizontal")); 
inputVector = inputVector.normalized; // Makes the Length of the vector = 1 even if pressed diagonally, if you don't want something like this, remove it 
rb.velocity = inputVector * speed; 

欲瞭解更多信息,請務必觀看此視頻:https://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player 對於更高級的版本閱讀:http://wiki.unity3d.com/index.php?title=RigidbodyFPSWalker