Day 101: Character Animation Part 4

Objective: Flip the character model to face the direction of movement.

We will start with flipping the character around to face the right direction when moving.

Where we are taking in the players input we will need to check to see if the player is moving. We do this by checking to see if “horizontal” is not equal to zero.

Next we will create a local Vector3 variable called facing and assign it the transform.localEulerAngles.

transform.localEulerAngles is the rotation numbers seen on the game object in the inspector.

We will check to see what way the character is facing by using a lambda expression. This expression is like a if statement just done in one line. We will be assigning facing.y to either 0 or 180 if _direction.z is greater or less than 0.

This lambda written out as an if statement looks like this. The lambda above and the if statement below do the same thing but in less lines. You can refer to Microsoft documentation. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions

Now we need to assign the transform.localEulerAngles to equal the facing value.

Now our character flips and faces the direction we are moving in!!
Thank you for reading and have a wonderful day!

--

--

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