- Home /
rigidbody. Velocity = Vector3.zero; ? help?
Okay, i was having this problem yesterday, but i have nothing to show you, so please try to understand me..
I may also say something that makes no sense, but i don't have my code in front of me..
So lets say i do rigidbody.addForce(transform.forward * speed); and i do the same but for transform.right, so now you're heading forward AND right, ya know, diagonally. But on release how can i make it zero out ONLY right? So lets say you release the "D" key how can i make my person still go forward but not right? sorry, i'm not the best with physics, my best guess was "rigidbody.Velocity = Vector3.zero" which obviously stopped velocity in all directions.
Answer by robertbu · Feb 26, 2014 at 07:31 AM
This is untested, but should work:
var dir = rigidbody.velocity;
dir = transform.InverseTransformDirection(dir);
dir.x = 0.0f;
rigidbody.velocity = transform.TransformDirection(dir);
It converts the velocity into local coordinates. There, the 'x' portion of the velocity is set to 0, and then it is converted back.
An alternate solution (again untested):
rigidbody.velocity = Vector3.Project(rigidbody.velocity, transform.forward);
The only change that has to be made for C# is to add 'f' to the 0.0.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134755im_/https://answers.unity.com/themes/thub/images/avi.jpg)