x


relative velocity of rigidbody according to rotation...

Hello all. I was messing around with programming tank treads. I'm simply using a texture that slides across a model to simulate treads. Thing is in order to move this I need to "fake" the treads moving along the ground. To do this I decided to just check if the treads are on the ground and use "rigidbody.AddRelativeForce" to move it forward when it is. So everything is working fine except one problem.

I don't know how to check the relative velocity to set a speed limit.

if (Input.GetKey(Forward)){ //Forward is a custom set key
if (Physics.CheckCapsule(transform.TransformPoint(0,-.6,-.4),transform.TransformPoint(0,-.6,.4),.09)){//This checks if you are on the ground
    if (rigidbody.velocity.magnitude < 5)//THIS is what I want to replace
       rigidbody.AddRelativeForce(Vector3(0,0,1)*Power);

}}

Right now I am simply using "rigidbody.velocity.magnitude" to check the speed, but that only gives me the total velocity, I want the velocity relative to the direction the treads are pointing. That way if I am moving at the speed of sound sideways, you can still push yourself forward instead of having to wait for the tank to slowdown so you can move where you want to go. Another problem is if you were being pushed backwards you wouldn't be able to push back which would really suck. Anyway to make the velocity magnitude nice and rotated to only look at one axis of movement relative to the object? Specifically the Z axis?

more ▼

asked Jul 22 '11 at 02:39 AM

MirrorIrorriM gravatar image

MirrorIrorriM
201 7 9 11

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

1 answer: sort voted first

You could just check the z component (rigidbody.velocity.z) instead of the magnitude.

more ▼

answered Jul 22 '11 at 02:53 AM

hellcats gravatar image

hellcats
651 8 12 23

rigidbody.velocity.z is relative to the world z axis. I need rigidbody.velocity.z but relative to the OBJECT'S z axis, which has been rotated.

Jul 22 '11 at 02:57 AM MirrorIrorriM

You could use transform.InverseTransformDirection(rigidbody.velocity) to transform the velocity into local space and then check the z component, or more efficiently you could compute the dot product of rigidbody.velocity and transform.forward using Vector3.Dot.

Jul 22 '11 at 04:07 AM hellcats

That's precisely the code I needed. Thanks!

Jul 22 '11 at 05:34 AM MirrorIrorriM
(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:

x2155
x1780
x316
x291
x83

asked: Jul 22 '11 at 02:39 AM

Seen: 2241 times

Last Updated: Jul 22 '11 at 05:34 AM