Ferenc Dajka
Ferenc Dajka

Reputation: 1051

Unity3D Input.getAxis() slowly dies instead of returning 0

I'm using Input.GetAxis() in Unity3D with keyboard (WASD), I printed out it's values in the FixedUpade() function, and when I release a button it returns values like: 1.0f, 0.7f, 0.4f, 0.0f, instead of instant 0.0f.

Is there a way to achieve instant 0.0f on releasing the button? (changing the value to 0 when it's smaller than 1 is not an option)

Upvotes: 0

Views: 1144

Answers (2)

ow3n
ow3n

Reputation: 6587

If you want the range of values that GetAxis() provides (smooth, graduated movement) but want the input value to return to zero immediately when the player releases the button/key (e.g. to halt animations), you can use GetAxisRaw() in your logic like so:

float inputX = 0;
    
// reset X after player has stopped pressing
if (Input.GetAxisRaw("Horizontal") != 0)
    inputX = Input.GetAxis("Horizontal");

Upvotes: 1

Bart
Bart

Reputation: 20030

GetAxis() will have smoothing applied to its values. If you want to use unfiltered data (0 or +/-1 for keyboards) use GetAxisRaw() instead.

Upvotes: 3

Related Questions