Character keeps moving a moment after I release input keys
I've searched but not found my exact problem so here it is:
Top down 2D game with a sprite character moving forward/backward/strafing with WASD/arrow keys. He looks at mouse. I move him with the following code (in Update). Using Unity 5.
transform.Translate ((transform.up*Input.GetAxis("Vertical") + transform.right*Input.GetAxis("Horizontal")).normalized *speed*Time.deltaTime);
The code works to move the character but the problem is the character keeps moving for about a half second after the input keys are released. This gives the controls a very unresponsive feel and the character seems as if he's slipping across the floor.
If I only tap the key, the effect isn't so bad, but if I hold it at all I get the problem. I should mention that if I hold the key for 1 second or for 60, he'll stop moving after the same amount of time (about a half second). It doesn't get worse the longer I hold, is what I mean to say.
Is there a better way to move the character? Put code in FixedUpdate but had the same problem.
Thanks for any help provided.
Answer by S_Darkwell · Mar 08, 2015 at 10:10 PM
That's because Input.GetAxis() returns a smoothed output.
Try using Input.GetAxisRaw(). Eg:
transform.Translate ((transform.up*Input.GetAxisRaw("Vertical") + transform.right*Input.GetAxisRaw("Horizontal")).normalized *speed*Time.deltaTime);
Hope that helps!
This fixes what I was looking for exactly. I think i see a lot of other posts about unity controller physics so I dont know how it works but my inclination is to say vote this answer up. Also do you know how to manipulate the smoothing so that i can set it to get something in the middle. I'll look myself also and post here if i find it.
Answer by bradclovell · Sep 05, 2016 at 08:28 PM
S_Darkwell's answer is right on, but there is also another solution besides Input.GetAxisRaw()
Although Input.GetAxis() returns a smoothed output that reaches 1 and -1 somewhat slowly and returns to 0 somewhat slowly, this can be sped up in the Edit/Project Settings/Input menu.
If you look at your axis, in the InputManager, there are options for Gravity and Sensitivity. Sensitivity is how fast the output reaches 1 or -1 Gravity is how fast the output returns to 0
If you raise these values, you can have smoothed GetAxis() input that is much more responsive.
Answer by JtheSpaceC · Aug 07, 2015 at 11:28 AM
I believe it worked better in FixedUpdate for me in the end, though this is an old project for me now..