How to zero out horizontal value instantly?

Hi,
I am making al little game were the movement of the player is done with transform.position += movement * Time.deltaTime; (‘movement’ is a variable for the horizontal input).
When you let go of the key, to go a certain direction, the velocity doesn’t go to zero instantly. This is no problem when you are playing because I like how it takes a little time to slow down, but it is a problem when I reset the game. When you press restart, the player goes a little to the side if he died while moving. Is there a way to zero out that movement?

You could try

if (!alive)
 movement = 0;

transform.position += movement * Time.deltaTime;

before the position change is made.

Hey @vvjoren,

You may be dealing with Unity smoothing your inputs, assuming your movement float is something like Input.GetAxis("Horizontal") * someSpeedMultiplier.

Check your input settings (Edit → Project Settings → Input Manager), specifically the Gravity, Sensitivity, and Snap settings for your movement axes. Try setting a high gravity value, like 999 to minimize input smoothing. Alternatively, you could use Input.GetAxisRaw() which doesn’t get smoothed.

See the Unity documentation on the Input Manager, GetAxis, and GetAxisRaw for more details.