Rigidbody.addforce the force keeps on adding.

I have this piece of code

` rigidbody.AddForce(new Vector3(Input.GetAxis("Horizontal") * 2, 0, 0)); `

The problem is that even if I release the Horizontal button, it will still keep adding force more and more.

The code is in update function, but it still shouldn't be happening because when axis is not pressed, it outputs 0 right?

Ok I found the problem, I debug logged the input axis and when you press it, it goes to 1 but when you release it, it doesn't go back to 0, but how do I fix that

Look at topBar: Edit->ProjectSettings->Input. Then open Horizontal in the Inspector and check that Gravity isn't 0.

Horizontal normally ramps from -1 (leftArrow) to 1 (rightArrow) and automatically drifts to 0 when you let go. The "drift to zero" speed is gravity. If yours was somehow knocked to 0, your horizontal will stick as you described. Higher is a faster snap to 0 (default is 3.)

But, the way you are doing things, I'd not use GetAxis("Horizontal"). It's designed with auto-falloff to make the character coast to a stop when you let go, instead of snapping to a halt. Since you're using it for acceleration instead of speed, you don't need that. In other words, you're simulating someone who gradually pushes and releases the gas pedal.

I think just `if(Input.GetKey("left")) rigidbody.addForce(-Vector3.right);` will give better results and will solve the problem.