x


How do I obtain the local velocity of a Rigid body on one axis?

I'm creating a simple driving game were the player controlled vehicle is a single box-like rigid body.

I am accelerating the vehicle with rigid.body.AddRelativeForce and I'm steering the object with rigid.body.MoveRotation.

I really need to be able to obtain the relative velocity along the vehicles local X axis (forwards and backwards) to be able to script rules and logic from, and right now all I seem able to do is get either the world velocity (rigidbody.velocity) or all-axis-combined velocity (rigidbody.velocity.magnitude) but neither of these are accurate enough for what I want to do.

Can anyone offer any hints here?

Thanks.

more ▼

asked Nov 02 '10 at 01:19 PM

Alkan gravatar image

Alkan
32 10 10 12

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Try (untested):

float xVel = transform.InverseTransformDirection(rigidbody.velocity).x;
more ▼

answered Nov 02 '10 at 03:59 PM

Jesse Anders gravatar image

Jesse Anders
7.3k 7 17 48

Just tested it, it works -- I've been doing this manually all this time. Thanks!

Nov 02 '10 at 04:08 PM VS48

That does exactly the same as the code I posted... (which works btw, just tested)

Nov 02 '10 at 04:10 PM Atnas1010

I don't seem to be getting the same result

Nov 02 '10 at 04:30 PM VS48

Hmm, I'm looking into that right now

Nov 02 '10 at 04:38 PM Atnas1010

Hehehe this should work: Quaternion.FromToRotation(transform.forward, Vector3.forward) * rigidbody.velocity); It was correct, just I think one of the axes was inverted

Nov 02 '10 at 04:50 PM VS48
(comments are locked)
10|3000 characters needed characters left

Is this what you are looking for?

(transform.rotation * rigidbody.velocity).x

more ▼

answered Nov 02 '10 at 02:19 PM

Atnas1010 gravatar image

Atnas1010
1.1k 6 10 26

(comments are locked)
10|3000 characters needed characters left

Use rigidbody.velocity.x. Is that what you're looking for?

more ▼

answered Nov 02 '10 at 01:40 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

No, rigidbody.velocity.x gives me the velocity on the world x axis, not the local axis - this means it can report anything from high to low values depending on the direction I'm driving the vehicle in.

Nov 02 '10 at 02:06 PM Alkan
(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:

x1788
x322
x318
x134
x3

asked: Nov 02 '10 at 01:19 PM

Seen: 4058 times

Last Updated: Feb 15 at 02:04 PM