When you are attempting to build a 2D platform game, one of the most important elements will help me a character movement. So in this post, I will show you how to make a simple animated character in Unity.
That character will have an idle animation, walking and running animations, and jumping and falling down animations.
I will be using a few assets make by Kenney so buckle up!
Create a project 💻
To begin, we have to start by creating a new 2D project. This will set Unity to work with the 2D environment.
Next would be to import some assets. I’ve used here these two:
Simple Environment 🏔
The next step would be to create a scene and set up a simple environment where we can start testing our movement.
We would need a background and some ground with colliders. Feel free to experiment 🤓
My scene looks like that:
And my colliders look like that:
For ground colliders, we will have to do one more step. Assign the correct layer to them, so it would be easier to detect if a character is on the ground.
To do it first, we have to create a new layer. You can do it by clicking on the Layer Dropdown in the Inspector under GameObject name and selecting “Add Layer…”
Now just enter a new User Layer and name it “Ground”. When you do that, select your ground colliders’ parent and change its layer to “Ground”.
Creating a character 🧍♀️
Now it’s time to create our animated character!
Create a new sprite on the scene, which will be our character. You can also assign a character idle sprite to it, so it would be easier to set the rest of the components.
Next, we need to add a Box Collider 2D and a Rigidbody 2D components to the character.
Additionally, we have to adjust the size and offset of the Box Collider 2D, so our character will walk on its feet.
Animating a character 📽
Kenney’s Toon Character pack comes with frame by frame animations that we can use for our purpose.
So, let’s use those and create some animations in Unity!
First, we have to create a new Animator Controller in which we will set up all the animations’ connections. Also, add it as a component to our character.
When you open the newly created Animator Controller, you won’t have much there. But we will change that.
First, we need a few parameters we would be able to tweak. Those will be “InAir” (bool), “Speed” (float), and “AirSpeed” (float).
Thanks to those parameters, we will be able to animate the character in idle, movement, and airborne states. And those states we have to create in the controller.
Idle will be a New Empty State, and Movement and Airborne states will be New Blend Trees.
Creating animations 🎞
We will get back to connecting them in a moment, but before that, we have to create our animations!
To make them, simply select our character on the scene and open the Animation window. You should see a big create button to create a new animation. If you already have some animations attached to the object, you will have to create an animation from the dropdown.
On that screen, you can also see a list of animations to create: “Idle”, “Walk”, “Run”, “Jump”, and “Fall”.
And in each of these animations, you can simply drag and drop a list of sprites to the timeline, and they should be assigned to the Sprite component in the Character GameObject.
Of course, pick the right one for the animation. 🤓
Setup Animator states 📁
With animations done, we can finalize our states in Animator Controller.
For Idle, we only have to assign the animation in the Motion field.
For movement, we have to double click on the state to enter it. There we will find actual Blend Tree.
We will use 1 dimensional Blend Type, and we will use Walk and Run Animations.
Additionally, you will have to disable Automate Thresholds and set thresholds values to 1 for Walk and 2 for Run.
Similarly, with Airborne state, we have to enter the Airborne state by double click. Here we will assign Jump and Fall animations and again tweak thresholds for them.
For Jump, we will have a value of 1, and for Fall, we will have a value of -1.
Transitions between Animator states 🔗
Now it’s finally time to connect our Animator states with each other!
Each of those lines is separate conditions, and it would be easiest if I just show it to you:
Idle – Movement
Idle – Airborne
Movement – Airborne
And we are done with Animator!
Continue reading with part 2 📖
This post is already a little bit lengthy, so I’m inviting you to continue reading with Animated Character 2D in Unity (Part 2).
See you there!
Or leave a comment if you have a problem with something here in the comment section below! 👨💻
You can also join my newsletter to don’t miss any future post! 🔥