Objective: Finish moving with animation.
Yesterday we set up the logic to move the player and flip the sprite to face the correct direction while running. Today we will cover setting up the animation.
We will start by creating our animation for running. With our Player Sprite selected and the animation window open, we will drop down Idle and create new clip.
Name the new animation how you would like, I named my Player_Run. After saving it we will drag in our sprite and set the sample to what fits the best for your animation.
Now that we created our animation, we will move to the animator window.
We will need to create Transition lines from Idle to Run and from Run to Idle.
We will need to set up the conditions for the animations to be between each other. For running, we will need a Float parameter named Move_Speed.
Now set the transition line’s up with the correct condition. We will want to move from Idle to Run if our speed is greater than zero and back to Idle if our Speed is less than .1. We will make sure Has Exit Time and Fixed Duration is off and Duration is set to zero.
Now for the logic. We will create another script called PlayerAnimation.
We will add the PlayerAnimation on the Player Object, like we did with the Player Script.
In our Animation script, we will need to get a reference to the animator on our Child Sprite object.
We will assign it to using GetComponentInChildren. We will also null check as well.
Next we will create a public method to set the float of the Move_speed in the animator. this method will take in a float variable that will be set from the player. We will use SetFloat(“Parameter name”, float variable.). We are using Mathf.Abs, this will return the absolute variable. We need the absolute variable to turn our negative value to positive. If we go left, our horizontal variable will be a negative number.
Now that we have our method for the running animation, let’s head to our Player Script. Before we call this method, we will need to get a hold of the PlayerAnimation script. We will complete this the same we have have with our rigidbody. Create the variable and then assign it.
At our location of assigning the player input we will call the RunAnimation method and pass in the horizontalInput.
Now our player runs left and right with a visual to show the running! Thank you for reading and have a wonderful day!!