How to Implement Movement in Different Genres of Games in Unity

In this very moving tutorial, learn how to implement movement from many different genres into your Unity games — both 2D and 3D. By Mauro Fuentes.

Leave a rating/review
Download materials
Save for later
Share

People tend to expect certain types of movement in certain types of games. If you’re playing a platformer, you expect to be able to run and jump. If you’re playing a Diablo clone, you expect to point and click to move your characters. So if you’re developing a game, it’s a good idea to know those rules of movement… even if it’s just so your mind-blowing game can break them!

In this tutorial, you’ll learn how to implement four types of movement in different genres in Unity. You’ll build a simple game with one backstory featuring two quirky characters, Catto and Ratto. See how they come to life in four different contexts:

  • 2D Platformers
  • 2D Top Down View
  • 3D Click to Move
  • 3D Tank Movement

This tutorial also requires that you have Unity 2019.1.3 or later installed.

Note: This tutorial assumes you have some basic Unity know-how and a bit of code-writing skill. If you need to go over the basics, read Getting Started with Unity and Introduction to Scripting in Unity.

This tutorial also requires that you have Unity 2019.1.3 or later installed.

Getting Started

Download the project using the Download Materials button at the top or bottom of this page, then unpack the files and open the starter project in Unity. Check out the folder structure in the Project view:

Structure of the Assets folder

This tutorial follows a top-down approach. Each subfolder contains everything you need to get started in each genre:

  • Models
  • Prefabs
  • Scenes
  • Scripts
  • Textures

Now that you’ve taken a look at your project materials, take a moment to think about the types of movement in the games you know and love.

Different Types of Movement in Games

While there’s a difference between game genres and the types of movement they use, the two concepts are so closely related that people often confuse them. That doesn’t mean that you couldn’t have a Resident Evil-style game where your character can fly, for example, but remember that restricted movement is part of what makes those games scary.

As with everything in art, you must learn the basics before creating your own style. Genres give you a starting point for choosing the movement types for your games.

Moving in 2D Versus 3D

Completely real 2D worlds are impossible. 2D and 3D are just models drawn to reflect people’s perceptions. Check out what Catto looks like in 2D and 3D:

Catto in 2D and 3D

In this tutorial, you’ll work with both 2D and 3D movement. But first, you need to learn about one important concept: Transforms.

Understanding Transforms

A Transform consist of 3 components. The Position, which is basically a point in 3D space. The Rotation, which defines the object’s orientation in 3D space. Finally, the Scale, which defines the object’s size.

The Transform's X, Y and Z axes

In Unity, you can see these 3 components of a Transform as follows:

The Transform component in the Inspector

Notice the values for Position, Rotation and Scale each have X, Y and Z values. Each of these X, Y and Z sets is known as a Vector3.

Now that you have that information under your belt, you can move on to your first type of game: 2D platformers.

Implementing 2D Platformer Movement

For your first step, you’ll work on creating movement in a 2D platformer. Some examples of published games in this field are: Super Mario Bros., Sonic, Castlevania, Ghouls ‘n Ghosts, Rayman Legends, Mega Man, Metroid and Ninja Gaiden.

Many people think that platformers are the easiest to code, just because they look simple. A character jumps from one platform onto another, how hard can that be, right? Actually, 2D platformers today can be very complex. Soon you’ll see why.

Setting Your Scene

Open the scene in RW/2D Platformer Movement/Scenes.

This is the Hierarchy view:

The 2D platformer Hierarchy

Expand Player Objects. You’re going to work on PaperCatto.

If you don’t see PaperCatto in the Scene view, here’s a sweet trick: Select it in the Hierarchy, press the F key in the Scene view, then press the 2D View button.

Just like this:

Finding and object with F in Unity

Adding Movement

There are two types of movements:

  • Translation movement is a point-to-point type of movement in a physical space. In other words, it makes the character move from position A to position B.
  • Emotional movement isn’t a technical term, but you’ll use it in this tutorial to cover movements the character makes without translating. For example, gestures, breathing and hand-waving are motions that convey intentions or feelings. Thus, emotional.

With PaperCatto still selected, give Catto some physical properties. This will allow Catto to translate, or move. Add a Rigidbody 2D:

Adding a Rigidbody 2D

To make Catto show feelings and attitudes, add an Animator:

How to add animator component

Finally, Catto needs a Box Collider 2D to be able to collide with other objects:

Adding a box collider to PaperCatto

Now press the Play button.

Catto gets hit

Oops, that looks uncomfortable! At least it works. :]

Customizing the Movement

So Catto can move now, but you still need to make some adjustments to make him move just right. Expand the Box Collider 2D and click the Edit Collider button.

Adjusting Catto box collider

You should now be able to edit the bounds of the 2D collision box. Expand the bounds of the collision box so that it covers Catto entirely.

Adjusting Catto collider 2d

You can click and drag the handles of the collision box to make it fit.

Change the Gravity Scale to 2 in the Rigidbody 2D. This controls the gravity amount that’s applied to Catto. A low value like 1 for example would make Catto jump extremely high, as if Catto was on the Moon. In this instance 2 makes the jumping force in this particular case feel much more natural.

Scaling the Gravity Rigidbody 2d

Finally, under Constraints, make sure you check Freeze Rotation Z. This prevents Catto from falling over while he runs around.

Implementing the Animator Controller

Last, but not least, hook up the Animator Controller. With PaperCatto selected, expand the Animator then select PaperCatto as the Controller:

Adding a Character Controller

Press Play and… Voilà! Isn’t that the most beautiful paper cat you’ve ever seen?

Catto standing on a pipe

Writing a 2D Platformer Movement Script

OK, so PaperCatto is moving, but you still need him to react correctly to the user’s input. To do that, you’ll need a script.

Imagine you want to create a Super Mario Bros.-like game starring Catto. What kind of movement do you need? Well, you’ll need to translate Catto on the X and Y axes and rotate him:

  • The X axis determines if Catto runs right or left.
  • The Y axis allows translation up and down for jumping and falling.
  • A rotation around the Y axis lets you rotate Catto so he faces the correct direction while moving.

Axis in 2D worlds

Here Catto is being translated along the X-axis, moving him from left to right and back again:

What is Translation

Here Catto is being rotated around the Y-axis, flipping him from left to right and back again:

What is reflection

Note: Instead of rotating Cato around the Y-axis to flip him from left to right, you could also scale him on the X-axis, where -1 will flip him left, and 1 will flip him right.