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?
3D Tank Movement
The idea that tanks have to stop moving to turn is widely spread across the Internet. That's why this kind of movement is called tank movement. However, the first tank was the Mark I, and it had good turning capabilities, even while moving. Contrary to popular belief, tanks can turn and move at the same time, and so do characters in games with this type of movement.
Anyway, here's the tank-type of movement in a nutshell:
- Up is always forward for the player, from the character's perspective.
- When you press left or right, the character performs a stationary turn.
Published games in this genre include:
,
and
.
Hooking up Elements
Open the scene in RW/3D Tank Movement/Scenes.
Select Tankatto in the Hierarchy under Player Objects.
You know the drill by now. Can you think of the components for this one, in 3D?
[spoiler title="Solution"]
Tankatto needs a Box Collider to interact with the world and a Rigidbody that the player will move and control.
[/spoiler]
Here's a visual aid:
Remember to edit the collider:
Go to 3D Tank Movement/Scripts and drag and drop TankMovement.cs onto Tankatto. Then open it so you can edit it.
Here's how it should look after adding the rest of the components:
Writing a Tank Movement Script
Your initial script has six different variables. As before, each relates to something you need to move or animate Catto. Three have to do with speed, one is the Rigidbody and two are for the input.
Add these variables at the beginning of the script:
public float tankSpeed;
public Rigidbody tankRigidBody;
public float movementInput;
public float turnInput;
public float movementSpeed;
public float turnSpeed;
A challenge: Which component will you cache during Start
?
[spoiler title="Solution"]
YAY, tankRigidBody
.
tankRigidBody = GetComponent<Rigidbody>();
[/spoiler]
Another mini challenge: Where would you cache turnInput
and movementInput
?
[spoiler title="Solution"]
That's right, during Update
.
[/spoiler]
turnInput = Input.GetAxis("Horizontal");
movementInput = Input.GetAxis("Vertical");
As you know, this type of game relies on two types of movement: turning and moving forward. So for your next step, you'll teach Catto how to turn.
Turning Catto
This time, you need to calculate the physics of Catto's movement in FixedUpdate, but you'll learn a clean way to do so. Instead of writing and managing all the code inside Unity's function, write it separately.
Add these short lines inside FixedUpdate
:
Move();
Turn();
As you see, the names of the functions are representative of the actions.
Now, add all this code to make Turn()
a nice separate function:
private void Turn()
{
float turn = turnInput * turnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
tankRigidBody.MoveRotation(tankRigidBody.rotation * turnRotation);
}
What happens here?
Take into account that turnInput
and turnSpeed
are read multiple times per second. You must take control of the situation.
So, you pass turnInput
, you multiply it by turnSpeed
to get a good number, and then restrain the time cycle to roughly a second via Time.deltaTime
.
fixedDeltaTime
, but it's outside the scope of this project.
This calculation gives you a refined number, which you simply call turn
.
The image below shows the flow:
Moving on to turnRotation
, you see a Quaternion.Euler
function. The theory behind Quaternions is vast and complicated. Suffice it to say, it has to do with rotation.
Unity's function, MoveRotation(Quaternion rot)
, forces you to use a Quaternion. But, wait, aren't rotations a Vector3 in Unity, as it says at the beginning of this tutorial?
Unity fixes that problem for you. Quaternion.Euler takes three numbers: X
,Y
and Z
.
In this context, can you guess on what axis you rotate Catto?
[spoiler title="Solution"]
If you guessed Y
, you would be correct! :]
[/spoiler]
You already have a number... the number in turn
. So, pass it as Y
into Quaternion.Euler(X, Y, Z)
.
It looks like this: Quaternion.Euler(0, turn, 0)
.
The last function MoveRotation
takes the current Tankatto's rotation and multiplies it by the turnRotation
you just calculated. Have a look at the flow:
Awesome!
Moving Catto
Now, the last step: Getting Tankatto moving forward. To start, create a new function called Move()
like so:
private void Move()
{
Vector3 movement = transform.forward * movementInput * tankSpeed * Time.deltaTime;
tankRigidBody.MovePosition(tankRigidBody.position + movement);
}
This last part takes care of movement... it's pretty much the same as the previous one.
As before, you store a variable that holds the input of your keyboard, the speed you defined through the Inspector and Time.deltaTime
.
Then, why is movement
a Vector3
this time? That's easy: transform.forward
is a Vector3(0, 0, 1)
.
Finally, MovePosition
takes movement
and adds it to Tankatto's current position.
Tankatto can't move yet. Remember to set the values in the Inspector. You can use these numbers, or you can adjust them as you see fit:
- Tank Speed = 6
- Movement Speed = 40
- Turn Speed = 60
Press Play and test if everything works as intended.
You might be thinking: Where's the fun in translating and rotating a tank?
Remember Tankatto has a Missile Logic component in the Inspector, but learning how it works isn't part of the tutorial. It's just there to spice things up, now that you've come this far. :]
Click your mouse and wreak havoc!
By the way, contrary to what it seems, no rats have been harmed in the making of this game genre.
Catto and Ratto are good friends. Missiles in this game are just dummies, the point of the game consists is to push the other off the platform.
Where to Go From Here?
Congratulations on finishing this tutorial! Remember, you can get the completed project by pressing the Download Materials button at the top or bottom of this tutorial.
Extra challenge!
Genre bender, baby! Try to mix two or more genres into one game. Mixing is the best way to discover new things. For example, try to bring PaperCatto into 3D Tank Movement as if it were a paper target. Also, you can try to make moving targets.
You definitely want to check this out to increase your chi as a game developer:
How to Create a Bomberman Game in Unity. Or, if you like videos, watch this impressive series by Brian Moakley Beginning C#.
Thanks so much for reading and following along with this tutorial. If you have any questions or comments, feel free to join the discussion below!