繁体   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