簡體   English   中英

Rigidbody.AddForce() 對象旋轉后不起作用

[英]Rigidbody.AddForce() not working after object is rotated

為什么這僅在 xInput 設置為 0 時才有效,而不管 zInput 是什么? (它是在一個自上而下的 3d 世界中,所以它只需要繞 y 軸旋轉)

rb = the animal's rigidbody
fr = a Vector3D to hold the rigidbody's direction
xInput = the steering wheel (a float between 0.0 and 1.0)
zInput = the gas pedal (a float between 0.0 and 1,0)

//rotate the animal
rb.transform.Rotate(0,xInput * turn_speed,0);

//get the direction the animal is facing
fb = rb.tranform.forward;

//apply force to the player in the forwards direction
rb.AddForce(fr * zInput * speed);

Debug.Log(fr * zInput * speed); //shows that the numbers are correct 
//(for example (0.5,0.0,-2.7)), but the animal doesn't move.

試圖制造一些神經網絡控制的動物。 這段代碼在我用鍵盤控制測試它時有效,但是當我將控制權交給中性網絡時(它只吐出兩個浮子,一個用於旋轉,一個用於速度),動物只是坐在一個地方笨拙地旋轉。 (偶爾它會先移動幾個像素,但不可避免地會停止)

解決了。 unity docs 提到你不應該嘗試同時操縱對象的變換和剛體,我正在這樣做。

我使用 rb.AddTorque 而不是 rb.transform.Rotate 並解決了問題。

暫無
暫無

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

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