How To Do 2D Top-Down Movement – Unity C#

So you want to make a top-down game, but you’re not sure how to handle character movement. I’ll assume you’re a beginner and explain how in detail.

The Code

First, attach a Rigidbody2D component to your 2D character, and turn the gravity scale down to 0. Next, make a new script and attach it to the character and add the following code inside your class:

 Rigidbody2D body;
 float horizontal;
 float vertical;
 float moveLimiter = 0.7f;
 public float runSpeed = 20; 

 void Start ()
 {
    body = GetComponent<Rigidbody2D>();
 }

void Update()
 {
    horizontal = Input.GetAxisRaw("Horizontal");
    vertical = Input.GetAxisRaw("Vertical"); 
 }

void FixedUpdate ()
 { 
    if(horizontal != 0 && vertical != 0) 
       body.velocity = new Vector2((horizontal * runSpeed) * moveLimiter , (vertical * runSpeed) * moveLimiter); 
    else 
       body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed); 
 }

Rigidbody

The Rigidbody2D is what handles collisions and physics for 2D characters. We get the component in Start to can access the velocity later.

Taking Input

horizontal and vertical will track which keys are being pressed on the keyboard, or what direction you’re pressing the control stick in. For keyboard controls, Input.GetAxisRaw(“Horizontal”) will give us a value of either -1 or 1. Left arrow is -1, aright arrow is 1. Vertical will be 1 for up and -1 for down.

If you’re using a control stick, instead of being either -1 or 1, you will get decimal values between that range depending on the direction of the stick.

When you multiply horizontal or vertical with runSpeed, the character will either be moving -20 or 20. This is how we determine if the character is moving left or right, because negative values will move you left (or down).

Update And Fixed Update

We get the input in Update so that we don’t miss key presses. Physics tasks, like changing velocity, should be done in Fixed Update, because it’s designed to calculate physics. We could have done movement simply like this:

body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);

It works, but the player will move faster diagonally. Instead, we check if you’re moving diagonally, and if you are, we reduce speed by 70%.

Velocity

Velocity is both a speed and a direction (this web page explains that). It is also a property of Rigidbody2D. When we set it to something other than 0, our character will start moving in a direction.

horizontal and vertical determine our direction, being either -1 or 1. Then when we multiply it by the runSpeed to determine how fast we move in the given direction.

Multiplying a number by 0.7 will give you 70% of the total. That’s why in our first if statement, if the player is moving diagonally, we multiply the value by 0.7.

Keyboard Or Controller

This scheme is intended for keyboard. For controllers, you don’t need to limit the movement speed because when you press diagonal on a controller it does that automatically.

So if you play to make your game work with both keyboards and controllers, check which input is being used and modify your code accordingly.

Conclusion

So you’ve got your character moving and you hopefully understand why it moves.  In a future tutorial I might go over how to also make your character face a different direction based upon the direction you’re moving.

I also made a puzzle game called The Nodus. Check it out.

Follow this blog for more game development tutorials and info about my upcoming games.

 

 

2 thoughts on “How To Do 2D Top-Down Movement – Unity C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s