2D Player Setup (2): Animating the Sprite!
The Player’s coming to life a bit more in this 2D mobile adventure game project 😉 This is mainly thanks to setting up the AnimatorController, the animations, and this ‘PlayerAnimation’ script (which handles and stores the bulk of the code needed to control the Animator component, as well as adjust the sprite according to which direction the player’s moving).
Animations
Unity makes setting up 2D animations pretty easy as long as we have the right assets. Once you’ve split your sprite sheet into multiple sprites (take a look at the ‘Adding Sprites’ section here if you’d like a quick refresher), we can then just create a new animation on our player’s sprite GameObject, then shift-click and drag the sprites we want to use into the dope sheet. You can then stretch this animation out in the Animator window till it plays at just the right speed for your purposes.
An ‘Animation State’ with the same name as the animation we just created should appear in the Animator component, but if not you can always just drag the Animation from your Assets/Project view into the Animator window.
Animator Controller
As we’ve done previously, this is mostly a matter of mapping out your animations and creating logical connections between various states. For instance, a “Jumping” boolean which takes the player from either the Idle or Running state and into the Jump Animation State. I’ve also got a “Velocity” boolean, which should probably just be called “Speed” in all honesty (this value essentially just keeps track of the horizontal input value). If Velocity is greater than 0.1, then we transition from Idle into Running, and vice versa if Velocity’s less than 0.1.
Admittedly, the “Jump” animation is a kinda tricky one. We can’t just play the whole animation in one go as that’ll likely have the player sprite ‘land’ in midair. Instead, I just cut the animation in half within the Animator window, then made a separate “Landing” animation which’ll play if the Player is no longer jumping.
I’d also kinda like a Falling animation, but to be honest I need to experiment with the logic on that a bit more (and probably take a look online to see how other people have reliably and appropriately called a “Fall” animation). For now, I’ve just implemented Idle, Run, Jump, Land, and Attack animations/animation states:
PlayerAnimation (script)
As touched on in the previous article, I’m calling a number of public methods on a “PlayerAnimation” script. This script manipulates the parameters on the Animator on cue, and also takes the horizontal input to decide which direction the sprite GameObject should face (by changing its scale on the X axis in this case). I’m less and less keen on serialised fields, so I haven’t included any with this script currently (I’m just seeking out the various components within the Start method). I should probably add in some null checks to this script, and would definitely do so if this was a collaborate project (as coworkers could unknowingly break things just by renaming the “Sprite” GameObject for instance). I’ll include the full script below:
With all of this set up, we just need to head back into Unity and ensure our hierarchy’s set up correctly. I’ve got the Player, PlayerAnimation, RigidBody2D, and BoxCollider2D components on the Player GameObject, and then the SpriteRenderer and Animator component on the child “Sprite” GameObject:
If all’s gone well, we should have a working, animated sprite for our Player! 😄 (As for the “SwordArc” and other attack stuff, I’ll be touching on that in the next article 😉)