- Home /
How do you directly add to velocity in a relative manner?
I'm trying to make a sort of booster-style function where you're instantly forced forward and up when the function is called. Since I wanted the process to be generally abrupt and not gradual, I didn't want to use rigidbody.AddRelativeForce, but I can't seem to figure out how to directly add to velocity in a way that affects it locally/relatively. I want it to force me forward based on my rotation, not along the world's Z axis.
There have been a lot of questions about relative velocity, and the answer seems to be "use Vector3.Dot" or "use transform.InverseTransformDirection". I don't know how to add to velocity locally by using those functions, however. It seems those who were posting questions about it only wanted to get your relative velocity, not add to your relative velocity.
I would prefer using velocity over AddRelativeForce, since AddRelativeForce is gradual and very hard to accurately control.
Also, I know the documentation says not to edit velocity directly, but I don't think that applies to me, as all I really use rigidbodies for is gravity (everything else physics-related is Translate-operated) and don't ever add force/torque in situation. Of course, feel free to correct me if I'm wrong here and it really will cause some kind of glitchy, unstable physics.
Hopefully this doesn't sound too much like a "write my script" question...I just really can't figure out how to go about this.
Thanks for any help you can offer!
Answer by Fattie · Oct 08, 2011 at 10:57 AM
Sure, it's easy:
// here is a very unusual case in game physics.
// we will simply "set the velocity" of the object.
// there are very few cases where this is appropriate
// but in this situation it is perfectly clever and good:
thingy.rigidbody.velocity =thingy.transform.forward * -5.0 ;
Just as you said: there are only a few, special, circumstances where you set the velocity directly. (You are "breaking" the physics - manipulating the fabric of reality!) In short, beginners working with 3D should never do this. Hope this is what you were after.
This seems to be working great. Thanks for the help and clarification!
In case someone wants to know in the future, this is the code I use to force you forward and up by predefined variables (ForwardForce and UpwardForce):
rigidbody.velocity += ((transform.forward ForwardForce) + (transform.up UpwardForce));
Your answer
Follow this Question
Related Questions
relative velocity of rigidbody according to rotation... 1 Answer
Rigidbody move relative position ? 2 Answers
Set The Local Velocity Of A Rigidbody 2 Answers
How to use local angular velocity? 1 Answer
Setting Local Velocity Of A RigidBody 3 Answers