简体   繁体   中英

Rigidbody.AddForce() not working after object is rotated

Why does this only work when xInput is set to 0, regardless of what zInput is? (It's in a 3d world that's top-down, so it only needs to rotate around the y-axis)

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.

Trying to make some neural-net controlled animals. This code was working when I tested it with keyboard controls, but when I hand over control to the neutral net (which just spits out two floats, one for rotation and one for velocity), the animal just sits in one place and awkwardly rotates. (occasionally it will move a few pixels first, but it inevitably stops)

Solved. The unity docs mentioned that you shouldn't try to manipulate both an object's transform and its rigidbody at the same time, which I was doing.

I used rb.AddTorque instead of rb.transform.Rotate and that fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM