Platformer: Player Animation Part 4

Ryan McCoach
3 min readDec 13, 2021

Intro

In this article, I will go over how to flip the player so it is facing the way it is moving.

The Code

Through code, we want to be able to change the rotation of player on the Y-axis. If the Y is rotated 90 it is facing right and -90 it is facing left.

Using the direction being calculated by the horizontal input, we can figure out which way we are moving: left or right: If the the left arrow key is pressed -1 is return and if the right arrow key is pressed 1 is return.These two values are used to determine the X value in the direction, basically if it is negative we are moving left and if it is positive we are moving right.

Lets access the rotation value by creating a local Vector3 variable called facing and setting it to the transform > localEulerAngles (rotation on the 3 axis). Now, we have a handle on the rotation angles we can change them depending on the direction our player is moving.

Putting this all together, if the direction of X is greater than 0 (positive horizontal input, moving right), we set the rotation angle on the Y to 90 which is facing right. If the direction of X is less than 0 (negative horizontal input, moving left), we set the rotation angle on the Y to -90 which is facing left. After that is rotation is determined, we just need to set the rotation to the facing value.

Conclusion

We still have a few more animations to add to have the player character come to life, but this simple flip truly makes it feel like you are in control of the player.

--

--