x


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!

more ▼

asked Jan 28 '10 at 07:40 PM

CheshireCat gravatar image

CheshireCat
13 1 1 5

Sorry to be dense, but where do you enter the rotation input for this? i.e. if you're using:

turning = Input.GetAxis ("Horizontal") ;

May 27 '10 at 07:09 AM TinyUtopia

I have a kinematic rigidbody..and I want to do rotate rigidbody in momentum and stop it then. How is that possible??

Jun 28 '10 at 11:44 AM Pria
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jan 28 '10 at 08:58 PM

Brian Kehrer gravatar image

Brian Kehrer
2.8k 9 11 50

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 instead.

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!).

Jan 28 '10 at 09:19 PM CheshireCat
(comments are locked)
10|3000 characters needed characters left

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;
more ▼

answered Jan 29 '10 at 12:45 AM

Horsman gravatar image

Horsman
426 3 6 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5075
x2163
x1788
x195

asked: Jan 28 '10 at 07:40 PM

Seen: 3803 times

Last Updated: Jan 29 '10 at 07:15 PM