2D Game Development – Sprite Animation
Day 063 #100DaysOfCode
Day 017 #100DaysOfGameDev
The world is set, stocked with objects, and there is character and an enemy who can move around. Despite all that, it’s very static. Time to learn how to add animation to sprites and get things moving.
To add movement to individual sprites, a new component must be added to the character prefabs: Animator. The most important setting on the Animator component is the Controller setting. A Controller is the component responsible for choosing which animation to play based on rules that will be defined, such as speed or direction of movement.
To get the animations the Controller will use, the Animation window must be opened. Once opened, you create an animation clip for each animation state you plan to use by loading the different sprites that will compose the animation.
With animations put together and assigned, the Controller needs to be set up to define how they will all be related. The Parameters defined here will be used by the script to give information to the Controller, while the Animation State Machine shows how they will transition to one another. Blend Trees come into play here to make things easier by blending multiple animations depending on the Parameter.
Now this all has to be integrated into the Controller script. This begins by ensuring the new Animator component is retrieved withing the Start function and stored inside a variable that can be used. With that in place, the Update function needs to be able to send the Parameter values to the Animator by way of the SetFloat function.
This is fairly straightforward for the EnemyController script, which only travels back and forth, but will get more complicated for the RubyController script because she will move around based on player input.