How to Move a 2D Character in Unity

2D Character Movement

For this 2D character tutorial, we are going to move a square character around your playable screen. This tutorial is going to introduce you to a few different ways that Unity allows you to move characters and objects around your screen using the keyboard.

Please keep in mind, that this is for 2D game development. This tutorial will show a beginner game developer how to move a character up, down, left and right on the game screen.

Tutorial Steps:

  1. Launch Unity and create a new project. Name it whatever you want (I named mine 2D Character Tut) and save it where you want. For ease of access, mine is on my desktop. Select the 2D bullet point so that it will launch as a 2D application and press the Create Project button.
  2. For my sanity, I always save my scene when I start. It’s a good habit to save your scene often, since crashes do and will happen.
  3. In your Hierarchy tab, click Create -> Create Empty. You see a game object called GameObject appear in your Hierarchy. Rename it to Character by selecting the game object, waiting a few seconds, select it again and type in the new name. Hit Enter to apply the new name.
  4. Now, we can’t see our game object on our Scene tab, so let’s fix that. With your Character selected under the Hierarchy tab, go to your Inspector and click Add Component -> Rendering ->Sprite Renderer.
  5. Under inspector, your Sprite Renderer has appeared. Where it says Sprite, next to the Text Box that reads None, there should be a circle. Click it.
  6. You are given some default Unity options to use as a sprite. You can choose whichever you like, or import your own. For this tutorial, I will just use the UISprite. So, double click UISprite. In your Game tab, you should now see a white dot. To auto zoom into the Character, double click it in your Hierarchy tab. We now have a basic character in Unity. Let’s move it around.
  7. Open up MonoDevelop (or similar). Click File -> New -> File… -> Click on Unity -> C# Unity Script. Name it Character, then click New.



  8. Your new script, pre laid out for Unity, should appear. Save it in your project Assets folder under the Scripts folder. If you don’t have a Scripts folder, create it. This is to help keep all of your assets organized. On a small project, no biggy, but on a game project, you don’t want to lose your stuff in the chaos of file naming. So make a Scripts folder and save your new C# file in it.
  9. Under your public class, we will add a float variable called speed. This will determine how fast your character will move.
    float speed = 1.0f;

     

  10. In your Update, you will declare a move variable, and how it will move with your transform position.
    void Update() {
             var move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
             transform.position += move * speed * Time.deltaTime;
         }
  11. Save your Character script then go back into Unity. It may take a second for Unity to update. Under your project tab in your Assets folder, you should see a new Scripts folder. Double Click to open it and you will find the Character script you just created.
  12. In your Hierarchy tab, select your Character. Now, click and drag your Character script into your Character Game Object Inspector. You have just attached your script to your game object.
  13. At the top of your screen is a play button. Press it. Use your arrow keys and you should see your character dot move around your screen. Exciting!

Subscribe for more tutorials!

Congratulations!

You have just made your character move around your screen. If your character is not moving around your screen using your arrow keys, please go back into MonoDevelop and check that you don’t have any typos. Of all the mistakes programmers make, a typo is the most common, and the hardest to find. So, always check your work.

unity-2d-character-movement-live-workshop-tutorial

Leave a Reply