- Home /
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?
How is the movement float set? Could you post more of the script?
Answer by FeedMyKids1 · Jun 30, 2020 at 05:13 PM
You could try
if (!alive)
movement = 0;
transform.position += movement * Time.deltaTime;
before the position change is made.
Answer by exploringunity · Jun 30, 2020 at 06:02 PM
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.
@exploringunity, It's a handy tip, but I like the smoothing when you play the game. I want to stop that smoothing when you die, so that the player doesn't move a bit to the right or left when you press restart and the time goes back to one.
Your answer
Follow this Question
Related Questions
How to make the accelerometer of object move more smoothly? 1 Answer
I do you control movements and apply other forces at the same time ? 0 Answers
How to get 'real' velocity of an object and not the input velocity? 2 Answers
rigidbody It's going up and bounces when is moving 1 Answer
Vector3 Issues - Not Able To Solve Constructor issue 1 Answer