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
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

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:

  1. Up is always forward for the player, from the character's perspective.
  2. 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:

Video showing how to hook up components in Unity

Remember to edit the collider:

Editing the Collider Box in Unity

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:

Tankatto's components in the Inspector

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.

Note: There's a 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:

Call a function separately

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:

The flow of code for MoveRotation

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!