Hey there, Gamers and Game Makers!
Welcome to the first in our Making a Basic Platformer Series. This tutorial series aims to give a basic introduction to the elements needed for a platformer game and how to make them. We're going to start by making our character controller that will allow us to move the player and make them jump. Let's get started.
As you can see below, I already have a very basic scene set up with my platforms and a weird little rectangle ghost dude that I made in Photoshop in a few seconds. Other than the sprites being placed in the scene there is no other set up done yet.
Ok, so the first thing we want to do is create a new C# script and open it in Mono Develop. This is going to be our character controller script and I'm going to name it as such.
You can go ahead and delete everything I've highlighted in the default script.
I'm going to start off by declaring some of the variables I'll need. I'm creating a private Rigidbody2D variable called RigBod that will be used as part of the 2D physics our character controller will have to make use of later. Next, I'm going to create three floats. JumpHeight which we will use as you may have guessed to control how high the player jumps. playerSpeed to control the speed of our player and a private float called playerInput which we will use to detect if the player is using the movement keys (arrow keys)
I'm now going to add a line in our start function that allows us to manipulate our players rigidbody via script.
Ok, on to the actual movement itself. We're going to start off by getting our player to move left and right. I do this by creating an update function called FixedUpdate that will set my playerInput variable equal to a get axis input. This simply checks if the player is pressing either the left or right arrow keys.
However, this does not actually move our player. We now need to set the velocity of our rigidbody. We also need to make sure we do not affect the Y axis with this particular input so at the end we simply say RigBod.velocity.y.
Once this is done, we can return to the Unity editor and actually apply this script to our player character and set some values. I'm going to start off by setting the speed to about 30 for now. We also need to add our Rigidbody 2D component to our player here. After doing so, make sure you tick the Freeze Rotation checkbox under constraints. We also need to add a 2D collider so that our player doesn't just pass through everything. Don't forget to assign colliders to the actual platforms themselves otherwise you might as well be jumping on a cloud.
If you play the scene now, you'll see that you'll be able to move the player left and right. However, you'll notice that the player is always facing the same direction. Not exactly the best if you want to move in both directions and have it look good. We'll use a little trick that will allow us to flip the sprite depending on which direction we want to move. Head back into the script.
First off, create a new bool variable called lookingRight and set it to true by default. Now, let's create a method that will actually flip the player sprite. Call it flipSprite and in the body of the method, set lookingRight to be equal to !lookingRight(equal to not lookingRight). Create a Vector3 Scaler and set it equal to the players local scale. This is the X,Y and Z values of the player at a given point in time in the scene. We then take the scaler of X and multiply it by -1. We thwn set the local transform equal to the Scaler. This method basically looks at the X value of the players transform when turning left. If the X value is 2 while facing right, to flip the sprite on the X axis we change this to -2.
This is all well and good but we still need to be able to check if we need this function and when to actually call it. In our FixedUpdate, add an if statement that checks if our lookingRight variable is equal to false and our playerInput is greater than zero. Basically this is checking if we are moving to the left. We then use the flipSprite(); method we created to do as you imagine and flip the sprite. We apply the same logic for turning right with an else if statement.
If you play the scene now, you'll see the sprite flips to face the direction you are moving in. Ok next up, we want to make our character jump. It wouldn't be much of a platformer if he didn't jump. We're first off going to need some new variables for this.
I'm first going to create a private bool called onPlatform which I will be using to tell if the player is actually standing on a platform. For this to work, we need something on the player that can be used to tell if the player is actually on the platform. Before we get to that, let's add the remaining variables needed. A public transform to check for a platform, a float to check radius and a LayerMask to determine what is a platform.
Back down in our FixedUpdate, we're going to set our onPlatform variable equal to Physics2D.overlapCircle. We need to tell it where to generate the circle and since we want this to be at the base of the player, we'll provide it with the platformCheck variable with .position. We then need to check the radius, so we'll provide that variable as an argument too and finally provide the LayerMask to determine the platform.
Back in the Unity editor, we'll need to create an empty game object to place at the base of the player. Make it a child of the player object and place it at the base of the player.The circle will now appear at this position. Make sure you assign that empty game object to the platform check component of the script in the editor. Give the radius an appropriate value. I'll use 0.2 for now.
In the Layers and Tags section of the editor you will need to create a layer that can be used to identify what a platform is. Once you have done so, apply this layer to all of your platforms.
Ok, time to head back into our script to finally get the player jumping. Start by creating an int variable called jumps. Then create an update method that will contain an if statement that will check if the player has pressed the space bar. What we are doing here is basically checking if the space bar has been pressed while you are on a platform. If so, once you jump we decrease the number of jumps you have so that you can't just keep jumping in mid-air forever. You can assign different values for the jump height, number of jumps and force in the inspector now.
If you play the scene now, you'll be able to jump around. Adjust your settings in the inspector until you get a jump that feels right for you. Finally, we are going to improve the overall smoothness of our players movement by preventing them from getting stuck on platforms. To do this, we will create a 2D physics material. Once you do this, set its friction to zero. Then assign this material to the players rigid body component.
Play the scene again and you should finally have a smooth playable 2D character controller. That does it for the first part of this tutorial series on making a basic 2D platformer. As always, feel free to get in touch if you have any comments or suggestions.
Until next time!