Player Setup Part 5
Objective: Set up Flip Behaviour for Player Sprite
As shown above, when the Player moves forward and backwards, the rotatio of the Player has it always facing to the right. We need the Player to face left when moving that direction.
There’s an easy way to set the sprite facing rotation and that is using the Sprite Renderers Flip functions. This mirrors the sprite in the X or Y axis. We will flip the X making the sprite face left.
There’s many ways to do this and it may be common to put this logic in the Player script but we have a seperate script to control the Players animations.
Open the PlayerAnimation script and firstly create a private SpriteRenderer and assign it in the Start method using GetComponentInChildren. You would generally null check but we can skip that for now. Next create a FlipSprite method that takes in a float which will be the move float value passed in from the Move method we created in the previous post. You could do this all in Move but this will keep things organised and cleaner for later. Call the method from inside Move and pass the float value. Now inside the FlipSprite first we will ceck that the value is not 0 meaning the Player is in idle then run another if to check if the value is greater than 0 and set the SpriteRenderer.flipX to false meaning don’t flip as we are moving forwards and run an else to set it to true meaning we are running backwards. That’s it, pretty simply.