Tank control
Tutorial
·
Beginner
·
+10XP
·
45 mins
·
(692)
Unity Technologies

Configure your tank’s movement with user input, collision detection, engine audio, and dirt trails behind its tracks.
1. Overview
In this tutorial, you’ll set up a functional Tank that can move, turn, and interact with its environment. By the end of this tutorial, your tank will have movement controls, sound effects for idling and driving, and visual dust trails behind its tracks.
2. Edit Playmode tint
By default, the Editor window around the Game view becomes slightly darker when you enter Play mode. However, this change is subtle and can be hard to notice. If you're unaware that you're in Play mode, you might accidentally make changes that won't be saved.
To prevent this and avoid losing work, we'll adjust the Play mode tint to make it more obvious when Play mode is active.
1. Choose the new playmode tint color
- From the main menu, select Edit > Preferences (macOS: Unity > Settings…).
- In the left menu, select Colors, then, under the General section, change the Playmode tint color to have a subtle color instead of gray.
- Enter Play mode to test the new tint.
The color should be very noticeable, but still allow you to read the text in the Editor window.
Note: You can also paste a Hex color code instead. In our case, we used the color #9DC2EF.
2. Fine tune your color choice
- If the color needs further adjustments, return to the Preferences (Settings…) window and edit it.
Important: Remember to exit Play mode before continuing to make more changes; if you don’t, your changes will be lost.
3. Add a Rigidbody component
To make the tank interact with Unity’s physics system, you need to add a Rigidbody component to the Tank GameObject. This component allows the tank to respond to forces and collisions. Without a Rigidbody component, the tank wouldn’t be able to move realistically in the game world.
Additionally, to ensure the tank remains stable and moves only in the intended directions, you can freeze its movement along the Y-axis and restrict unwanted rotations. Unity uses a Y-up coordinate system, meaning the Y-axis represents vertical movement. By freezing Position Y, you can prevent the tank from floating or sinking. You’ll freeze Rotation X and Z to stop the tank from tilting sideways, ensuring it only rotates around the Y-axis to turn left or right.
1. Add a Rigidbody component to the Tank
- In the Hierarchy window, select the Tank GameObject.
- In the Inspector window, select Add Component.
- Search for “Rigidbody” (not Rigidbody 2D) and select it.
2. Test the Rigidbody component
- Select the Play button in the Unity Editor.
- Observe that the tank falls through the ground due to gravity.
3. Apply movement constraints
- In the Inspector window, use the foldout (triangle) to expand the Constraints section of the Rigidbody component.
- Enable Freeze Position Y to prevent the tank from moving vertically.
- Enable Freeze Rotation X and Freeze Rotation Z to keep the tank from tilting sideways.
4. Test again to confirm constraints
- Select the Play button and ensure that the tank no longer falls through the ground and remains upright.
- If needed, use Ctrl+Z (macOS: Cmd+Z) to undo mistakes.
The Rigidbody component is now applied to the tank, allowing it to interact with Unity’s physics system while ensuring that it remains stable and moves only in the intended directions.
4. Add and test the TankMovement script
To make the tank move and turn with input controls, you need to add the TankMovement script. Unlike Unity’s built-in components, we’ve provided this custom script for you, which is specifically designed for this Tanks project. This script will allow the tank to receive player input and respond accordingly.
1. Add the TankMovement script to the Tank
- Locate the TankMovement script in _Tanks > Scripts > Tank.
- Drag and drop the TankMovement script onto the Tank GameObject in the Hierarchy window.
- Select the Tank GameObject and verify that the Tank Movement (Script) component appears in the Inspector window.
2. Test the Tank’s movement
- Select the Play button and use the WASD keys to move the tank.
- Observe how the tank responds to input.
Note: The camera will not follow the tank yet. This will be addressed in a later tutorial.
3. Adjust Speed and Turn Speed values
- If the tank moves too fast or too slow, adjust the Speed property in the Tank Movement (Script) component.
- If turning feels too sharp or sluggish, adjust the Turn Speed property in the Tank Movement (Script) component.
Important: After you edit a value in the Inspector window during Play mode, switch to the Game view to make the WASD controls work.
4. Experiment with Direct Control
- Enable the Is Direct Control property in the Tank Movement (Script) component and test the movement.
- Enabled: The tank moves in the direction it is facing, similar to joystick-based movement.
- Disabled: Movement follows the traditional WASD key layout.
- Determine which control style you prefer.
Note: The Direct Control property is useful for mobile devices where joystick-based movement is more intuitive. It allows the player to move the tank by pointing in a direction rather than using separate forward and turn inputs.
Important: Changes made in Play mode are temporary and will reset once you exit Play mode. If you find settings you like, you'll need to take note of them before exiting Play mode.
The tank is now player-controlled, allowing for movement and turning, though additional improvements will be made in the next steps.
5. Troubleshoot Tank movement issues
If your tank doesn’t move and you encounter errors such as the following after adding the script, ensure that the Input Actions are set up correctly.
NullReferenceException: Object reference not set to an instance of an objectIn Unity, Input Actions define how user inputs (such as keyboard or controller inputs) are mapped to in-game actions. These settings are stored in a file and can be shared across projects.
1. Troubleshoot tank movement
- In the Project window, use the foldouts (triangles) to open the _Tanks > Settings folders and select the Tank_Actions file. In the Inspector window, check if the Assign as the Project-wide Input Actions button is available. If so, select it.
Once assigned, the Input Actions should be properly configured, and your tank should function as expected.
6. Add a Collider component
Right now, the tank moves through buildings and other objects because it lacks a Collider component. A Collider component defines a GameObject’s physical boundaries, allowing it to interact properly with the environment. In this step, you'll add a Box Collider component to the tank and adjust its shape for accurate collisions.
1. Add a Box Collider component to the tank
- Select the Tank GameObject.
- Select Add Component in the Inspector window.
- Search for “Box Collider” and select it.
Note: A Box Collider component is used instead of a Mesh Collider component for better performance, even though it’s not as accurate a shape.
2. Frame the tank for better visibility
- Double-click the Tank GameObject in the Hierarchy to center it in the Scene view. Alternatively, select the Tank GameObject and press F to frame it in the Scene view.
- Use the scroll wheel to zoom in and out as needed.
3. Adjust the collider to fit the tank
- In the Inspector window, in the Box Collider component, select the Edit Collider button.
- A faint green box will appear around the tank, representing the Box Collider component.
- Drag the collider handles to resize and reshape the box.
Important: Ensure that the Box Collider component slightly overlaps with the ground; otherwise, any force applied to the tank after a collision will persist indefinitely, as if in zero gravity.
4. Improve accuracy with different view angles
- The collider may not actually be perfectly aligned, even if it looks aligned from one angle.
- Use the Scene Gizmo to rotate the view.
- Switch to Orthographic view for more precise adjustments:
- Select the Scene gizmo and select a side, top, or front view.
- This makes it easier to see if the collider fits the tank’s shape correctly.
- Adjust the Box Collider component to fit the tank's main body as closely as possible.
- Right-click the Scene Gizmo and select Free mode to return to a more natural viewing angle.
Note: The goal is not to make a perfect outline of the tank, but to approximate its shape.
5. Return to Play mode and test the Box Collider component
- Select the Play button to enter Play mode
- Move the tank towards a building or object. It should now collide instead of passing through.
6. Exit Play mode
- Exit Play mode before making further modifications.
Important: Any changes made in Play mode will be lost when you exit Play mode.
The tank now has a collision boundary, preventing it from moving through other objects in the scene.
7. Add an Audio Source component
To make your tank produce an engine idling sound, you need to add an Audio Source component. This acts like a speaker that plays sound effects.
1. Add an Audio Source component
- Select the Tank GameObject in the Hierarchy window.
- In the Inspector window, select Add Component.
- Search for “Audio Source” and select it.
2. Assign the Audio Clip
- Select the Audio Resource picker (⊙).
- Select EngineIdle from the available options.
- Enable the Loop checkbox so the engine sound plays continuously.
Select the Play button to test the sound.
At this point, your tank will produce an idling sound, but the console error will persist, and the script won’t yet be able to switch to a driving sound when you move it using player inputs. You’ll fix this in the next step.
8. Assign Public Variables in the Inspector window
The TankMovement script is designed to switch between an idling sound and a driving sound based on the tank's movement. However, for this to work, the script needs to know which Audio Source component and Audio Clips to use.
1. Assign the Audio Source component
- Select the Tank GameObject in the Hierarchy window.
- In the Inspector window, locate the TankMovement (Script) component.
- Select the Movement Audio picker (⊙).
- In the Select Audio Source window, select Tank.
2. Assign the engine sounds
- Select the Engine Idling picker (⊙) and select EngineIdle.
- Select the Engine Driving picker (⊙) and select EngineDriving.
- Adjust the Pitch Range by increasing the value to add randomness to the sound for a more natural effect.
3. Enter Play mode to test
- Select the Play button.
The engine should now switch from idle to driving sounds as the tank moves and the error in the console should now disappear.
Troubleshooting
If the sounds do not play correctly, double-check the following:
- The Movement Audio property has the Tank GameObject’s Audio Source component assigned properly.
- The correct Engine Idle and Engine Driving Audio Clips are selected.
Your tank now plays an engine sound while idling and smoothly transitions to a driving sound when moving.
9. Add Dust Trail prefabs
To enhance the visual realism of the tank's movement, you’ll add Dust Trail prefabs behind the tank’s wheels. These are particle effects that create a dust trail dynamically as the tank moves, making its motion feel more realistic.
1. Add the prefab to the scene
- Open the Project window and open the _Tanks > Prefabs > Dust Trails folders.
- Select a Dust Trail prefab that matches your terrain:
- Moon, Jungle, or Desert.
- Drag the selected Dust Trail prefab onto the Tank GameObject in the Hierarchy window.
This makes the dust trail a child GameObject of the Tank GameObject so that it moves with it.
2. Position the dust trail
- In the Inspector window, set its Transform component’s Position property to X = 0, Y = 0, and Z = 0.
- Use the position arrows to place the effect behind the tank’s right track.
Tip: Press F while the dust trail is selected to frame it in the Scene view for precise positioning.
- Rename the prefab “DustTrail_Right”.
3. Duplicate the dust trail for the left side
- Right-click DustTrail_Right and select Duplicate (Ctrl+D (macOS: Cmd+D)).
- Rename the new copy “DustTrail_Left”.
- Move it behind the tank’s left track.
Note: If you’re using the MegaBall, Droid or UFO models, they don’t have distinct right and left sides. Instead, you can place a single Dust Trail prefab directly behind them.
4. Press Play to test
- The dust trails should now appear only when the tank is moving.
Your tank now generates dust trails as it moves, adding a realistic touch to its motion.
10. Create a Tank prefab
Converting your Tank GameObject into a prefab allows you to reuse it easily. Any changes made to the prefab will update all instances in your game.
1. Create a new folder
- In the Project window, open the _Tanks > Prefabs folders.
- Create a Tanks folder:
- Right-click in the Prefabs folder, select Create > Folder, and name it “Tanks”.
2. Convert the Tank GameObject into a prefab
- Select the Tank GameObject in the Hierarchy window.
- Drag it into the Tanks folder in the Project window.
- Verify that the Tank GameObject in the Hierarchy window now has a blue cube icon, indicating it is a prefab instance.
3. Save your scene by pressing Ctrl+S (macOS: Cmd+S)
Your Tank GameObject is now a prefab, allowing you to create multiple copies while keeping them linked.
11. Optional: More things to try
Try these optional activities to challenge yourself, build your skills, and improve your project!
Each challenge is tagged as Easy, Medium, or Expert difficulty so that you know what to expect. You can do one of them, all of them, or none of them — it’s totally up to you!
Easy: Adjust tank movement variables
Fine-tuning movement values is an essential part of game design. Experimenting with different settings helps ensure that the tank moves as expected and feels right for gameplay.
- Select your Tank GameObject in the Hierarchy window and locate the Tank Movement (Script) component in the Inspector window.
- Enter Play mode and test different values for Speed and Turn Speed. Adjust these settings to achieve the movement style you prefer.

Important: Changes made in Play mode aren’t saved once you exit Play mode, so take notes on the values that feel best so you can apply them once you exit Play mode.
Exit Play mode and update the Speed and Turn Speed properties with your chosen values in the Inspector window.
Medium: Explore dust trail Particle System settings
The Particle System component in Unity allows you to create dynamic visual effects, such as the dust trail behind your tank. You can modify various properties to change its appearance and behavior.
- In the Project window, select the DustTrail_[Theme] prefab you chose to view its Particle System component in the Inspector window.
- Hover over any property name in the Inspector window to see a tooltip, which provides a brief explanation of what that property does.
- Adjusting some key properties:
- Start Lifetime – Determines how long particles remain visible before fading.
- Start Size – Defines the range for the sizes of the newly created dust particles. Each particle is assigned a random size within this range when it spawns.
- Max Particles – Limits how many particles are emitted per second. Emission will be temporality halted when reaching this number.

- Enter Play mode to test your changes in real time. Adjust values until the dust trail looks natural and responsive to the tank’s movement.
If you’re not interested in doing these challenges, mark this step as complete.
12. Optional: Review the TankMovement script
This step is entirely optional, but if you’re curious to understand how the TankMovement script works, you can find it in the _Tanks > Scripts > Tank folder and open it in an integrated development environment (IDE) or text editor. The code is thoroughly commented, which helps as you read through it.
Below are a few aspects of the script that you might find particularly interesting.
1. Declaration of public variables
Defines key tank movement settings and references, making them accessible in the Unity Inspector window.
public int m_PlayerNumber = 1; // Used to identify which tank belongs to which player. This is set by this tank's manager.
public float m_Speed = 12f; // How fast the tank moves forward and back.
public float m_TurnSpeed = 180f; // How fast the tank turns in degrees per second.
public bool m_IsDirectControl;
public AudioSource m_MovementAudio; // Reference to the audio source used to play engine sounds. NB: different to the shooting audio source.
public AudioClip m_EngineIdling; // Audio to play when the tank isn't moving.
public AudioClip m_EngineDriving; // Audio to play when the tank is moving.
public float m_PitchRange = 0.2f; // The amount by which the pitch of the engine noises can vary.
public bool m_IsComputerControlled = false; // Is this tank player or computer controlled- Defines movement properties (m_Speed, m_TurnSpeed) and control settings (m_IsDirectControl, m_IsComputerControlled).
- Stores references to sound components (m_MovementAudio, m_EngineIdling, m_EngineDriving).
2. Engine sound logic
Determines and plays the appropriate engine sound based on movement state.
private void EngineAudio ()
{
// If there is no input (the tank is stationary)...
if (Mathf.Abs (m_MovementInputValue) < 0.1f && Mathf.Abs (m_TurnInputValue) < 0.1f)
{
// ... and if the audio source is currently playing the driving clip...
if (m_MovementAudio.clip == m_EngineDriving)
{
// ... change the clip to idling and play it.
m_MovementAudio.clip = m_EngineIdling;
m_MovementAudio.pitch = Random.Range (m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
m_MovementAudio.Play ();
}
}
else
{
// Otherwise if the tank is moving and if the idling clip is currently playing...
if (m_MovementAudio.clip == m_EngineIdling)
{
// ... change the clip to driving and play.
m_MovementAudio.clip = m_EngineDriving;
m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
m_MovementAudio.Play();
}
}
}- If the tank is stationary (m_MovementInputValue and m_TurnInputValue near zero), it switches to the idling sound.
- If the tank is moving, it switches to the driving sound.
- Randomizes pitch (Random.Range) to add variety to engine sounds.
13. Next steps
Congratulations!
With these steps completed, your tank is fully configured and controllable.
In the next tutorial, you’ll set up the camera to follow and zoom on the tanks as they move around the scene.