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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How to Implement Movement in Different Genres of Games in Unity
35 mins
- Getting Started
- Different Types of Movement in Games
- Moving in 2D Versus 3D
- Understanding Transforms
- Implementing 2D Platformer Movement
- Setting Your Scene
- Adding Movement
- Customizing the Movement
- Implementing the Animator Controller
- Writing a 2D Platformer Movement Script
- Moving Catto Right and Left
- Giving Catto the Ability to Jump
- Finishing the Movement
- Adding Conditions to the Movement
- Flipping Catto
- Flipping Catto to Match His Movements
- Coding 2D Top Down Movement
- Hooking up Elements
- Writing a 2D Top Down Movement Script
- 3D Click to Rotate and Move
- Hooking up Elements
- 3D Tank Movement
- Hooking up Elements
- Writing a Tank Movement Script
- Turning Catto
- Moving Catto
- Where to Go From Here?
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.
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:
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:
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.
In Unity, you can see these 3 components of a Transform as follows:
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:
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:
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 positionB
. - 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:
To make Catto show feelings and attitudes, add an Animator:
Finally, Catto needs a Box Collider 2D to be able to collide with other objects:
Now press the Play button.
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.
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.
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.
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:
Press Play and… Voilà! Isn’t that the most beautiful paper cat you’ve ever seen?
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.
Here Catto is being translated along the X-axis, moving him from left to right and back again:
Here Catto is being rotated around the Y-axis, flipping him from left to right and back again: