[Solved]Force on one axis only

Hello, I have a moving rigidbody and I want to apply force to only one axis of it.
If I use the AddForce, then it just “adds” up the force and not completely resets old one and applies the new one. Any suggestions?

You can assign to Rigidbody.velocity rather than use AddForce:

rigidbody.veloity = Vector3.right * speed;

Or you can jack up ‘Drag’ setting in the Rigidbody component and apply force every frame. With a high drag, the old velocity will decay quickly. This will be a bit more realistic than assigning to Rigidbody.velocity.

I think in the inspector for the Rigidbody you can freeze the position of the gameObject to the axis you want. So I suppose that if you freeze the rigidBody of the gameObject in the Y axis, then the force will only affect the X and Y axis and so.

EDIT: freeze position is under the “Constraints” section and there’s also a constraint to freeze the rotation.

Vector3 velocity = rigidbody.velocity;
velocity.x = (Vector3.right * speed).x;
rigidbody.velocity = velocity;

Remember to use FixedUpdate for this.