Platformer: Player Animation Part 4

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.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Everything you need to know about Caching — System Design

Scrum adoption pitfalls

Exploiting Remote File Inclusion with SMB

Binary Tree Right Side View — Day 73(Python)

Five must have bots for Microsoft Teams

Navigate data protection and risk in the cloud era

SFDX Org Authorization ☑️

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ryan McCoach

Ryan McCoach

More from Medium

2D Mobile Game: Player Run Animation

Unity highlight text instead of button

Creating A Modular Powerup System in Unity

Tap & Hold & Rapid Prototyping w/ New Unity Input System