2D Player Animation (part 4)
Adding the Jump Animation to the player
Now that we have our flip mechanic completed along with the move and and jump mechanic finished, it is time to add in the Jump animation to our player. We will also be learning how to create select certain frames to smooth out the jump animation.
Objective: Add an Jump animation to the player
Player Animation Setup:
Once you have sliced and divided the animation frames from the animation sheet, we are going to select only a few frames to incorporate into the animation only.
In the animation sheet, we will noticed that this animation sequence has the player squat downward before initiating the jump and also has frames for the player landing, but since our player jumps and lands so quickly, having that initial squat is unnecessary.
Instead we are going to only add a few frames that simulate the beginnings of the jump and have the last frame hold for a longer duration to create that “gliding effect”.
Next in the Animator window, we need to create a new Bool parameter and set that parameter as a new condition on when to toggle the jump animation.
I also created transitions from the Running state to the Jump state along with Idle to Jump since both these states are able to toggle the jump at any time. For the transitions, I set the conditions to the jumping boolean. “True” for when the player is transitioning to the Jump animation and “False” for when they return back to the original state.
Player & Player Animation Script:
Since we are housing all of the animation toggles within the player animation script, need to create a boolean switch from the player script to the player animation script to toggle the animator parameters.
In the Player Animation script we are going to create a new public method which will control the Jumping animation. Since we have already created the references from the move method, we don’t need to relink the animator component.
In the new public method we are going to create an If statement which determines whenever the set bool is either true or false, and with either parameter is togged the proper animation would be displayed.
We will also be adding a bool for the method so we can pass down the bool data from the player script.
In the player script within the movement method, during the Space keycode input we are going to toggle the jump animation. At the same time we are going to pass down the bool as “true” to let the player animation script know and play the jump animation.
To stop the jumping animation from looping itself and have the player return back to either idle or Running animation, we need to set the jump bool to become “false” when the player is grounded. This is perfect since we already have a bool method called “IsGrounded” that checks whenever the player is grounded.