How to tell when and in which direction the player is moving? (2d)

So, I am working on a game, and I am ready to add animations. My plan is to have a different animation play for each direction the player moves in. Like, in pseudocode, this:

if(player is moving left){
        PlayAnimation(left);
}

And, to play no animation when the player isn’t moving.
I know how to make the actual animation, but as for the movement thing… Not really.
Any idea on how to execute this?

1 Answer

1

There is not enough information to give you an answer, plus this question has been answered a number of times. If using a Rigidbody, see Rigidbody.velocity. If using a CharacterController, see CharacterController.velocity. If using transform, you will need to generate the vector by subtracting the current position from the previous position, or by using the movement vector.