- Home /
Horizontal/Vertical input immediately zero after key release
Hi.
I use the horizontal/vertical input axis to change the direction of the player. Therefore I want the direction to change immediately after the button is released. The problem is that after release, the value for the axis will slowly fall from 1 (or -1) to 0, which causes the change in direction to trigger about a second too late.
I thought about checking if the value for the axis is 1 / -1, and changing the direction if it isn't (that would be the case after release of the button), but then I would have the same problem for the beginning (When I press the button, the value slowly increases from 0 to 1.
Another idea was to handle the movement via script (With KeyUp / KeyDown events), but I want the player to have the dynamic key allocation of the Input manager (otherwise I could simply check for WASD or arrow keys). So is there a way to
a) Set the value of the axis to 0 after the key is released
or
b) Get the buttons which are allocated for the horizontal / vertical input axis
Thanks
I forgot: If there is a way to only have -1, 0 and 1 as input values (without the slopes), that would not be possible, because I need those values in between for a smooth acceleration.
Answer by Bunny83 · Mar 31, 2016 at 01:57 AM
That's pretty simple. Just open up the Input manager in Unity and assign a high gravity value for your input axes. Gravity is the "speed" for "falling to neutral" and "Sensitivity" is the speed for the acceleration.
Further more you can also tick the "snap" checkbox if you want. It will snap the value back to neutral whenever the player switches to the opposite key. Usually when you switch the direction without ever releasing both keys, the value will only be affected by sensitivity. So it you have a relatively slow acceleration it takes some time to actually fall back to neutral before you actually accelerate into the opposite direction. Snap will ensure you start accelerating at "0" / neutral.
Say it takes 1 sec to accelerate to the value "1.0" (which means you have a sensitivity of "1" unit per sec.). If you press the positive button it will accelerate and reach the value of 1 after 1 sec. If you now switch the direction (press the negative button at release the positive) you will "accelerate" into the opposite direction. However since the current value is 1.0 it takes 1 sec to actually reach 0 before the value actually becomes negative. With "Snap" enabled the value immediately returns to 0 when you switch an start accelerating from there.
Additionally, Input.GetAxisRaw() specifically ignores the gravity and sensitivity values applied to the input. This can be handy for situational 1:1 feedback from your controls.
Exactly what I wanted, and awesome explanation. I didn't know what gravity and sensitivity are, but now I do! Thanks.
I tried setting the gravity to a high value but it looks like it's not doing anything. I keep getting my walk animation after releasing the "w" key.
Is there a way around the bug... I $$anonymous$$ean any solution you know about it!!! If you know then also pls let me know...