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.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Player Death And How To Handle It

Open the Player.cs script in your code editor.

Right now, there's no variable to indicate if the player is dead or alive, so add a boolean variable at the top of the script, right under the canMove variable:

public bool dead = false;

This variable is used to keep track if the player died to an explosion.

Next, add this above all other variable declarations:

public GlobalStateManager globalManager;

This is a reference to the GlobalStateManager, a script that is notified of all player deaths and determines which player won.

Inside OnTriggerEnter(), there's already a check to see if the player was hit by an explosion, but all it does right now is log it in the console window.

Add this snippet under the Debug.Log call:

dead = true; // 1
globalManager.PlayerDied(playerNumber); // 2
Destroy(gameObject); // 3  

This piece of code does the following things:

  1. Sets the dead variable so you can keep track of the player's death.
  2. Notifies the global state manager that the player died.
  3. Destroys the player GameObject.

Save this file and return to the Unity editor. You'll need to link the GlobalStateManager to both players:

  • In the hierarchy window, select both Player GameObjects.
  • Drag the Global State Manager GameObject into their Global Manager slots.

LinkGlobalStateManager

Run the scene again and make sure one of the players is obliterated by an explosion.

HitByExplosion

Every player that gets in the way of an explosion dies instantly.

The game doesn't know who won though because the GlobalStateManager doesn't use the information it received yet. Time to change that.

Declare the Winner

Open up GlobalStateManager.cs in your code editor.

For the GlobalStateManager to keep track of what player(s) died, you need two variables. Add these at the top of the script above PlayerDied():

private int deadPlayers = 0;
private int deadPlayerNumber = -1;  

First off, deadPlayers will hold the amount of players that died. The deadPlayerNumber is set once the first player dies, and it indicates which one it was.

Now that you have this set up, you can add the actual logic. In PlayerDied(), add this piece of code:

deadPlayers++; // 1

if (deadPlayers == 1) 
{ // 2
    deadPlayerNumber = playerNumber; // 3
    Invoke("CheckPlayersDeath", .3f); // 4
}  

This snippet does the following:

  1. Adds one dead player.
  2. If this is the first player that died...
  3. It sets the dead player number to the player that died first.
  4. Checks if the other player also died or if just one bit the dust after 0.3 seconds.

That last delay is crucial for allowing a draw check. If you checked right away, you might not see that everybody died. 0.3 seconds is sufficient to determine if everybody died.

Win, Lose or Draw

You've made it to the very last section! Here you create the logic behind choosing between a win or a draw!

Make a new method named CheckPlayersDeath in the GlobalStateManager script:

void CheckPlayersDeath() 
{
  // 1
  if (deadPlayers == 1) 
  { 
    // 2
    if (deadPlayerNumber == 1) 
    { 
      Debug.Log("Player 2 is the winner!");
    // 3
    } 
    else 
    { 
      Debug.Log("Player 1 is the winner!");
    }
     // 4
  } 
  else 
  { 
    Debug.Log("The game ended in a draw!");
  }
}  

This is the logic behind the different if-statements in this method:

  1. A single player died and he's the loser.
  2. Player 1 died so Player 2 is the winner.
  3. Player 2 died so Player 1 is the winner.
  4. Both players died, so it's a draw.

Save your code, then give the game a final run and test if the console window reads out what player won or if it ended up as a draw:

FInalRun

And that concludes this tutorial! Now go ask a friend if they want to share in the fun and blow them up when you get the chance. :]

Where To Go From Here?

Download the finished Bomberman tutorial Final Project if you got stuck.

Now you know how to make a basic Bomberman-like game by using Unity.

This Bomberman tutorial used some particle systems for the bomb and the explosion, if you want to learn more about particle systems, check out my Introduction To Unity: Particle Systems Tutorial.

I highly encourage you to keep working on this game -- make it your own by adding new features! Here are some suggestions:

  • Make the bombs "pushable", so you can escape bombs next to you and push them towards your opponent
  • Limit the amount of bombs that can be dropped
  • Make it easy to quickly restart the game
  • Add breakable blocks that get destroyed by the explosions
  • Create interesting powerups
  • Add lives, or a way to earn them
  • UI elements to indicate what player won
  • Find a way to allow more players

Be sure to share your creations here, I'd love to see what you guys can come up with! As always, I hope you enjoyed this tutorial!

If you have any remarks or questions, you can do so in the Comments section.

Brian Broom

Contributors

Brian Broom

Author

Eric Van de Kerckhove

Author and Team Lead

Gijs Bannenberg

Tech Editor

Chris Belanger

Editor

Sean Duffy

Final Pass Editor

Over 300 content creators. Join our team.