- Home /
How to detect if player had switched direction of movement?
I'm making a 2d platformer. The character has the ability to walk and run. I am trying to implement mechanics that if the player is running and suddenly reverses the running direction, the character locks the player's control for 0.65 seconds and plays an animation of skidding before allowing the player to change direction. I've tried to do it like that, but the results are wildly broken, it doesn't trigger at the correct moment but always triggers in random other moments, like when the player is trying to go from idle to running or from running to walking.
//"moved" is the value of the player's input, "velocity" is the character's current velocity
speedDifferential = Mathf.Abs(moved.x) + Mathf.Abs(velocity.x / maxSpeed);
if (speedDifferential < 1.9 && Mathf.Abs(moved.x) > 0.2f && Mathf.Abs(velocity.x) > 2.5f && maxSpeed == SprintSpeed)
{
animator.SetTrigger("Stop");
AbruptStop = true;
Cooldowntimer = 0.65f;
}
I am not sure how your movement exactly work but can't you do something with the player input moved. I assume you use input axes. If so check if the input axes of moving is either 1 or -1. and set a bool to true and if it go above a certain threshold play the stopping animation and set the bool back to false.
for example your input axis get to 1. a bool is set to true and check if if you input axis become lower than -0.2.
Yeah, I ended up with something similar, but I used the bool that told which direction the player facing ins$$anonymous$$d. So far seems to work.