- Home /
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?
Answer by hellcats · Jul 22, 2011 at 02:53 AM
You could just check the z component (rigidbody.velocity.z) instead of the magnitude.
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.
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.