A collection of knowledge I wish I had when I started making games
When I started developing games with unity, I obviously wanted to do a big 2D RPG that would revolutionize the industry (well me and a lot of other people probably…). As you may expect, I failed miserably.
My first mission doing this big RPG was pretty obvious, I needed to move my character. I googled and with a one line of code my character was moving. At the time I was happy with it, but when I started learning more about the engine and game development in general I found out that moving the character in a game shouldn't be done with just one line of code.
So I decided to write this quick article to share knowledge behind 2D character movement using unity. I'm definitely not an expert on the subject so this isn’t going to be a tutorial of any sort. It's just condensed information that I wish I had when I made my first game.
So you want to move your character
As I mentioned above, you can move your character with just one line of code:
myRigidbody2D.velocity = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
Using rigidbodies to move characters in Unity is where I bet most people start, because it’s easy to code, to understand what’s happening and it simply get the job done. As I later found out, using a full physics engine (like Box2D which is the one Unity uses under the hood) for moving characters in a 2D game isn’t highly recommended. For example if you are creating a plataformer you may need precision on your movement and functionalities that a physics engine doesn’t offer you by default.
The Rigidbody2D component is designed to be used primarily for realistic physics simulations. You can see that by the multiple 2D physics components that are already made available by unity:
So you decide to create your own custom character controller and you start googling how to do it. However you discover that in comparison to just using the built in physics engine it’s really hard to write the script to move your character.
So now you face the dilemma of moving your character with a few lines of code and knowingly doing the wrong thing or spend a lot of time learning on how to create a custom script to make your character to move. And in your game movement isn’t even the main mechanic! Maybe you have cool attacks ideas and you are frustrated because you have spend a lot of time just to move your character only then do awesome attacks.
So my solution to you is: Hack away the physics engine until is good enough for your game. When you have a deeper understanding of game development in general you can code your own character controller. Because let’s be honest, your first game isn’t going to be a masterpiece, you probably even isn’t going to finish it.
I’ll try now to show a few tricks on how to use the physics engine to move your character and at the end I’ll link some good resources for further reading. If you want to learn about custom character controllers, feel free to skip the next section.
Here is where most people begin, so I’ll start here as well. I’ll try to avoid writing full pieces of code as this isn’t a full tutorial on how to do it. There are a lot of other sources on which you can go and copy their code, but I highly recommend it doing trying to do it yourself. I’ll try to focus on the general idea behind and show some tricks along the way.
The Rigidbody2D component.
In order to hack the Physics2D, you need to learn about the Rigidbody2D component. I recommend doing a quick reading of the documentation. You just need to have a grasp and understand what happens when you add this component to your Game Object. Also while you are at it, read the BoxCollider2D manual.
One small configuration you probably want to do right from the start so your sprite doesn't rotate around is freezing the Z rotation:
The big advantage of using Physics2D is it handles the all the collisions for us. So with minimal set up we can make two objects collide. Better than that, we can configure properties that will decide which object will with certain object.
With a simple physics configuration combining rigidbodies and 2D colliders we can create our world. I'll list now some features that you might want to add to your game and the resource needed to achive them.
You can simply move our character by altering the velocity attribute on the rigid body component. For example if we want to move it horizontally:
myRigidbody2D.velocity = new Vector2(Input.GetAxis("Horizontal") * playerSpeed, myRigidbody2D.velocity.y);
If you want it to jump:
myRigidbody2D.velocity = new Vector2(myRigidbody2D.velocity.x, jumpVelocity);
If you want to check if the player is on the ground so he can’t jump on the air, use the IsTouchingLayers function from the Collider2D component.
If you want platforms, use the PlatformEffector2D
You want to build your world using tiles and automatically set collision on them? Check this official tutorial from unity.
But remember to use composite collider 2D in order to avoid ghost vertices.
You want to avoid you character getting stuck on walls and adjust the friction? Create your own physics 2D material.
The only problem that requires a little bit of math is dealing with slopes. If really want to implement terrain with inclination in your game (if you are just beginning, I suggest you just to ignore slopes) you will have the following problem:
As you can see the player doesn’t follow the ground, it keeps his velocity and jump instead of going down. Also the player can’t slide down a descending slope.
The solution is to use raycasts and the normal vector to adjust the Player. This tutorial should hopefully give you the foundation to code a solution yourself.
Now you have the resources necessary to build your own platformer. Depending the type of game you want to do you might find the necessity to hack a little bit, inserting some corner cases that overrule the physics behavior. However you can implement a simple platform movement in minutes and focus your time on the core of your game. If you feel the necessity later to change how to movement is done, you haven't invested much time on it, so it's no big deal.
Using a custom character controller
Hey, now you know how to implement a lot of 2D movement features and you are ready to develop your game. So why should you scratch this 5-minutes physic implementation for a math heavy one.
More control over the player movement:
If the movement is a big part of your game there will probably be a time where the hacks aren’t enough to get the level of control you desire over your character.
Sometimes it doesn't feel right
When you play your game you probably will feel it is a bit sluggish comparing to something like Mario. You can hack your way to a better game feel, but if you want greate game feel you might want to spend some time learning the proper way.
Box2D has some limitations. For example depending on the velocity of the object collisions may fail. If your game require high speed characters, maybe you need to code it yourself.
Requires more from your computer
It’s expensive to use a complete physics engine for just platform movement. The physics engine brings over a lot of unnecessary features that you probably aren’t using.
If you want to read a more in depth opinion on why using physics is a terrible idea you can read this article.
So you as a developer must consider the benefits of writing your own movement script versus just using the built in physics engine that comes with Unity.
You are now hyped up to write your own code to move your character trough platforms. Where should you start?
I won’t go in depth here because this article is pretty big already so I’ll leave some links:
If you are interested in the subject I recommend you reading this article first. It's one of the best articles I found on the subject and it's a must.
Coding wise you should probably watch Sebastian Lague playlist on the subject. It's really well done and should help you write your own code.
Unity has an official tutorial on the subject aswell.
If you want to know more I enjoyed reading these three as well. They are a little bit more math heavy, but have interesting information.
Well I hope this article was useful for you. If you have any doubts feel free to send me a message.