How to last value of Get Axis after the button is released
Hi all
I'm trying to make my top down character stay in idle position after all the buttons have been released. I've created a blend tree with all the animation needed in 8 direction.
I get my animation blend tree parameters from the Controller script with the following code:
_movement = new Vector2(Input.GetAxis("MoveHorizontal"), Input.GetAxis("MoveVertical")); //I make a character move <br>
_animator.SetFloat(FaceX, _controller._movement.x);//I path the parameter to the blend tree
_animator.SetFloat(FaceY, _controller._movement.y);
And everything is working just fine when I move my character with keyboard or gamepad.
However the issue i'm facing is the following: After I release the buttons character returns to a default position (as GetAxis at this moment are equal to zero both x and y).
Is there a way to get "the last axis value before releasing an axis button" in Unity?
Answer by AltaNox · Jan 10, 2020 at 09:59 PM
Don't use the values the axis provide to directly set the movement, instead, add them up over time and clamp the values to whatever you need.
float inputSpeed = 2f;
Vector2 input = new Vector2(Input.GetAxis("MoveHorizontal"), Input.GetAxis("MoveVertical"));
input.Normalize();
_movement += input * Time.deltaTime * inputSpeed;
_movement.x = Mathf.Clamp(_movement.x, -1f, 1f);
_movement.y = Mathf.Clamp(_movement.y, -1f, 1f);