How To Make A Game Like Bomberman With Unity
Ever want to blow up your friends? Learn how to make a game like a Bomberman with Unity 3D in this step by step tutorial that will have you setting bombs off with ease. By Brian Broom.
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 Make A Game Like Bomberman With Unity
25 mins
Unfortunately, it’s a little difficult to secure C4 explosives along with some willing buddies willing to explore the afterlife. Thankfully, there are some alternatives.
Enter this Bomberman tutorial. Bomberman is a game where four players battle it out by strategically placing bombs across the battlefield with the goal being to blow each other up.
Each bomb has a few seconds of delay before it explodes and spews out an inferno in four directions. For additional excitement, explosions can trigger impressive chain reactions.
The original Bomberman came out in the early 80s and spinoffs have been published ever since. It’s a timeless game formula that’s still a lot of fun to play and build.
The original title was 2D, but you’re going to create a basic 3D version inside of Unity.
In this tutorial, you’ll learn the following:
- Dropping bombs and snapping them to a tile position.
- Spawning explosions by using raycasts to check for free tiles.
- Handling explosions colliding with the player.
- Handling explosions colliding with bombs.
- Handling player death(s) to determine a win/draw.
Loosen up your wrists and get ready to shout “fire in the hole”. Things are about to get really explody inside of Unity. :]
Getting Started with this Bomberman tutorial
Download the Starter Project for this Bomberman tutorial and extract it to a location of your choosing.
Open up the Starter Project in Unity and start this Bomberman tutorial. The assets are sorted inside several folders:
- Animation Controllers: Holds the player animation controller, including the logic to animate the players’ limbs when they walk around. If you need to brush up on animation, check out our Introduction to Unity Animation tutorial
- Materials: Contains the block material for the level
- Models: Holds the player, level and bomb models, as well as their materials
- Music: Contains the soundtrack
- Physics Materials: Holds the physics material of the players — these are special kinds of materials that add physical properties to surfaces. For this tutorial it’s used to allow the players to move effortlessly arund the level without friction.
- Prefabs: Contains the bomb and explosion prefabs
- Scenes: Holds the game scene
- Scripts: Contains the starter scripts; be sure to open them and read through them because they’re heavily commented to make them easier to understand
- Sound Effects: Holds the sound effects for the bomb and explosion
- Textures: Contains both player textures
Dropping A Bomb
If it’s not opened yet, open up the Game scene and give it a run.
Both players can walk around the map using either the WASD keys and the arrow keys.
Normally, when player 1 (the red one) presses Space he should place a bomb at his feet, player 2 should be able to do the same thing by pressing Enter/Return.
However, that doesn’t work yet. You need to implement the code for placing bombs first, so open the Player.cs script in your favorite code editor.
This script handles all player movement and animation logic. It also includes a method named DropBomb that simply checks if the bombPrefab GameObject is attached:
private void DropBomb()
{
if (bombPrefab)
{ //Check if bomb prefab is assigned first
}
}
To make a bomb drop beneath the player, add the following line inside the if
statement:
Instantiate(bombPrefab, myTransform.position, bombPrefab.transform.rotation);
This will make a bomb spawn at the player’s feet. Save your changes to the script and then give your scene a run to try it out:
It’s working great!
There’s a small problem with the way the bombs get dropped though, you can drop them wherever you want and this will create some problems when you need to calculate where the explosions should spawn.
You’ll learn the specifics of why this is important when this tutorial covers how to make the explosions.
Snapping
The next task it to make sure the bombs snap into position when dropped so they align nicely with the grid on the floor. Each tile on this grid is 1×1, so it’s fairly easy to make this change.
In Player.cs, edit the Instantiate()
you have just added to DropBomb()
like so:
Instantiate(bombPrefab, new Vector3(Mathf.RoundToInt(myTransform.position.x),
bombPrefab.transform.position.y, Mathf.RoundToInt(myTransform.position.z)),
bombPrefab.transform.rotation);
Mathf.RoundToInt
calls for the x and z values of the player position, rounds off any float to an int value, which then snaps the bombs to the tile positions:
Save your changes, play the scene and run around while dropping some bombs. The bombs will now snap into place:
Although dropping bombs on the map is pretty fun, you know it’s really all about the explosions! Time to add some firepower to this thing. :]
Creating Explosions
To start off, you’re going to need a new script:
- Select the Scripts folder in the Project view.
- Press the Create button.
- Select C# Script.
- Name the newly created script Bomb.
Now attach the Bomb script to the Bomb prefab:
- In the Prefabs folder, select the Bomb GameObject.
- In the Inspector window click the Add Component button.
- Type bomb in the search box.
- Select the Bomb script you just made.
Finally, open the Bomb script in your code editor. Inside of Start(), add the following line of code:
Invoke("Explode", 3f);
Invoke()
takes 2 parameters, firstly the name of the method you want to be called and secondly, the delay before it gets called. In this case, you want to make the bomb explode in three seconds, so you call Explode()
— you’ll add it next.
Add the following under Update()
:
void Explode()
{
}
Before you can spawn any Explosion GameObjects
, you’ll need a public variable of the type GameObject
so you can assign an Explosion
prefab in the Editor. Add the following right above Start()
:
public GameObject explosionPrefab;
Save your file and return to the Editor. Select the Bomb prefab in the Prefabs folder and drag the Explosion prefab to the Explosion Prefab slot:
Once you’ve done this, return to the code editor. You finally get to write the code that makes things go boom!
Inside Explode()
, add the following lines:
Instantiate(explosionPrefab, transform.position, Quaternion.identity); //1
GetComponent<MeshRenderer>().enabled = false; //2
transform.Find("Collider").gameObject.SetActive(false); //3
Destroy(gameObject, .3f); //4
This piece of code does the following:
- Spawns an explosion at the bomb’s position.
- Disables the mesh renderer, making the bomb invisible.
- Disables the collider, allowing players to move through and walk into an explosion.
- Destroys the bomb after 0.3 seconds; this ensures all explosions will spawn before the
GameObject
is destroyed.
Save your Bomb script and return to the editor and give your game a play. Put down down some bombs and bask in the fiery goodness as they explode!