- Home /
How might I rotate rigidbody momentum?
Hi,
I currently have a rigidbody spaceship that is controlled by the player. I have assumed that the spaceship should be a non-kinematic rigidbody because I want it to bounce and rotate during collisions. Please let me know if this is wrong.
The player can use the up and down arrow keys to give the rigidbody momentum (the keys enable and disable thrust). This acts like acceleration, by calling rigidbody.AddRelativeForce . This was exactly what I wanted.
I then went and made it so the player could turn the rigidbody, by implementing rigidbody.AddRelativeTorque . This did not achieve the desired effect, as the directional vector already in place caused the rigidbody to drift.
My question is: how do I rotate this rigidbody such that momentum rotates with the object? More precisely, is there a function that will do it for me, or should I use the math component to manually rotate the force vector on the rigidbody?
Thanks,
Andrew
EDIT:
Special thanks to Brian Kehrer and Horsman for putting me on the right track. I slightly modified their solution, by multiplying the original vector magnitude by the directional vector, rather than the transform vector, which yielded all sorts of crazy results.
rigidbody.velocity = Vector3.forward * rigidbody.velocity.magnitude;
Thanks for the help!
Sorry to be dense, but where do you enter the rotation input for this? i.e. if you're using:
turning = Input.GetAxis ("Horizontal") ;
I have a kinematic rigidbody..and I want to do rotate rigidbody in momentum and stop it then. How is that possible??
Answer by Horsman · Jan 29, 2010 at 12:45 AM
It is not physically acurate to do this, of course, but you can acheive it easily by rotating the current velocity of the ship.
Vector3 velocity = rigidbody.velocity;
rigidbody.velocity = Vector3.zero;
rigidbody.velocity = transform.forward * velocity.magnitude;
Basically this ought to do the trick:
rigidbody.velocity = transform.forward * rigidbody.velocity.magnitude;
Answer by Brian-Kehrer · Jan 28, 2010 at 08:58 PM
It's not physical, but you could just rotate the velocity vector and reapply it. This typically results in more intuitive, better feeling controls.
EDIT The easiest way to do this is maintain a transform as the current intended velocity vector. Rotate the transform when the player rotates, and then call Transform.forward.
Multiply that by the Rigidbody.velocity.magnitude and reassign that to Rigidbody.velocity.
That is what I'd meant when I mentioned manually rotating the vector. I wanted to make sure there wasn't a function that would do this for me ins$$anonymous$$d.
On the other hand, I have absolutely no idea what the best way to rotate a vector is given the API (I'm new here!).
Answer by paranoidray · Feb 29, 2016 at 04:32 AM
I wanted to model a bird flying. I am using this in the mouse controller:
transform.Rotate(Vector3.up, x * xSpeed);
rigidBody.velocity = Quaternion.Euler(0, x*xSpeed, 0) * rigidBody.velocity;
where x is
Input.GetAxis("Mouse X");
In the WASD script I use:
m_Rigidbody.AddForce(f * 10.0f * Camera.main.transform.forward + s * 10.0f * Camera.main.transform.right);
f is forward button and s is strafe button press
int f = Input.GetKey(KeyCode.W) ? 1 : Input.GetKey(KeyCode.S) ? -1 : 0;
int s = Input.GetKey(KeyCode.D) ? 1 : Input.GetKey(KeyCode.A) ? -1 : 0;
Answer by Virepri · Aug 16, 2018 at 08:41 PM
I actually solved this by getting the change in the angle I'm trying to rotate on.
For reference, rb is rigidbody, pc is my camera script. prevRot is the rotation before the mouse moved.
rb.velocity = Quaternion.Euler(0, transform.eulerAngles.y - pc.prevRot, 0) * rb.velocity;