2D Character Movement Unity 3D Tutorial

2D Character Movement

In this tutorial, we are going to move a block around your playable screen. This tutorial is to introduce you to a few different ways that Unity 3D 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.

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 in to 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!

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.

Next Tutorial

For the next tutorial, we will build on this one, and add some collision for your character. It’s fun to walk into stuff, so let’s do it!

Have a Question?

Contact me or comment below.

Want to Request a Tutorial?

Contact me or comment below.

Stay in Touch with Eimear Studios

Eimear Studios loves to be social with you! Hit me up below, or check out some of my other social pages to stay up to date with what I’m doing.

Like us!            Circle us!           Tweet us!        Watch us!           Feed us!
social icons_facebook social icons_google-plus social icons_twitter social icons_youtube social icons_rss-feed

 

 

 

We have lots of social-ness to share! If you need more, and we know you do, check out more of our social networks in the footer of our page!

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

 




 

Leave a Reply

Your email address will not be published. Required fields are marked *