I'm working on a top-down game where the player can move up/down/left/right. The sprite changes to the corresponding direction, and I have an internal value that tracks the direction the player is facing.
The actual movement is handled just by doing
rigidbody2D.MovePosition(player.position + movement * speed * Time.deltaTime); and it's working fine. Direction is determined by looking at the absolute values of all 4 axis values and seeing which one is greatest.
The problem I'm having is when using an analog joystick on a controller. So, if you suddenly let go of the stick, it has a little "bounce" where it goes past the center, and into the opposite direction.
I uploaded a slow-motion video of the joystick being released, to show this "bounce."
I added some logging to the input, and here are some numbers:
The joystick bounce always ends up being somewhere between < 0.1 to up to 0.5, for maybe 1 frame in time.
What can I do to mitigate the effects of this bounce? It's causing my player to suddenly look in the opposite direction if they suddenly stop moving.
I've had these ideas for solutions, but I'd love some advice:
- Increase deadzone to be over 0.5 - This seems like an easy fix, but that feels like it's too big of a deadzone.
- Ignore input if the difference between last frame and current frame is greater than a specific value (maybe 0.5?)
- Average out input over the last few frames and then move? Seems like it could be buggy though (and feel laggy)