What we have done here is set up the sprite for our actual player as it’s own object as a child of the player object. This allows us to keep the inspector more clean when adding in a bunch of different components to our player. To start, we will need to create a rigid body for our player to work with:
The issue now is that our player just falls once the game ahs started. To fix this, we will want to add a box collider onto our player, but also make a few adjustments to our tile map so that they can have colliders built in them as well. To get started, we will want to add a tilemap collider:
Once we have added in the tilemap collider above, we can see that everything that is considered an object within that section has now been given it’s own collider. As we have a lot of unnecessary colliders within our section, let’s clean it up a little bit by adding in a composite collider along with adjusting a few settings:
What we have done here is within our rigidbody, we have the body type set to static. In the Tilemap collider, we checked off our Use By Composite option and now we get ourselves an nicely outlined collider.
Now that we have our game setup with the required colliders, it’s time we go into the script of our player and get the movement part set up. As for what we will be starting with, it will just be our basic movement:
As we can see from our pseudocode, we are just looking at creating some basic horizontal movement within our player:
As we can see above, we created a connection to the rigidbody so that we can control the movement through there. From there, we set our hInput to the input of the horizontal keys. Finally, we set the velocity of our object as the hInput:
As we can see from the short test above, our character has a slight slide to it. This is because of how Unity calculates the movement. With the setup of GetAxis, unity slowly moves up to 1 and then back down to 0 slowly. Due to this process, we experience a slide with our character. To solve this, we will make a quick adjustment to the script:
With the use of GetAxisRaw, the value of the input will be the whole number, and not just a gradual climb to it:
Now we can see how it is a much smoother stop, which will benefit us later when we start to add in the varying animations for our player.
Next up, let’s take a look at how we can get our character jumping. As it will have some of the same aspects of our platformer, we will use that same principle here, but in a 2D aspect instead:
So, what we have done above is cleaned up our Update statement so that it has our movement method alone in is as best practice. It is always best to try and keep our update method as clean as possible and create new voids to run our codes in. From there, we are going to create a Jump method in which we call it if we press Space along with the fact our IsGrounded void is called. In order to have our method call properly, we will create a quick Coroutine to allow our code to breath a little between activation so that it doesn’t trigger it to go on and off in the same frame. As for our IsGrounded method, what we had to do is find a way to allow our ray to detect just the ground and not the player and everything else in the scene. In order to do this, we need to utilize a bit shift method. This method allows us to choose a layer mask, meaning we need to assign the floor that we are on to a specific layer:
Finally, we will have our code return if it is triggered allowing us to jump, otherwise if we try to jump while not grounded, the bool will just run again:
The last part of our basic movement will be quickly adding in a way for our player to run quicker than his current pace, which we can look to manipulate in the future:
Now that we have our basics set up with the character, we can look towards the next part and adding in some animations to how our character acts within the game.