← All articles
Game DevelopmentPublished 29 July 2020 · Updated 16 March 2024

Collision and Health in GameMaker

Learn how to set up your player and enemy health and how to destroy them based on some collision rules. Beginners game design tutorial 10

Tutorial 10: Collisions and Health Setup

In this tutorial, we will learn how to set up your player and enemy health and how to destroy them based on some collision rules.

Set the player collision mask

Go to the Resources menu:

  1. Open Sprites. Open your player sprite, sprPlayer
  2. Find Collision Mask in the Sprite window. Open this area by clicking the small arrow beside the words
  3. In the Collision Mask area, from Type, choose Precise (Slow)

A screenshot of a cell phone Description automatically generated

Set up player health

  1. Open the player object, objPlayer, from the Resource menu
  2. Go to the Create event and add the following code:

myLives = 3; //maximum number of lives
maxHealth = 10; //this sets up a counter for you players health. Increase or decrease it if you like

currentHealth = maxHealth;

Collision with enemy ship

Create a collision event on the player object with the enemy ship and add this code:

currentHealth -= 2;

collision event in gamemaker studio 2

Set up a collision with the enemy laser

collision event with the enemy laser

  1. On the player object, add a collision event with the enemy laser (as seen above).
  2. Inside the event add this code
currentHealth -= 1; //take some health off the player
with(other){
	instance_destroy(); //destroy the enemy laser when it touches the player
}

Set up a step event to check the status of the player’s health

In the Step event for your player object add the following code:

if(currentHealth<=0){
   myLives-=1; //take 1 life off everytime your health reaches 0

if(myLives <=0){ //only destroy the player when lives reaches 0
  room_goto(EndRoom1); //we will create this in the next tutorial
   instance_destroy();
}else{
currentHealth = maxHealth;
instance_destroy(objEnemy1);
instance_destroy(objEnemyLazer);

  }
}

Enemy health setup

Do the same steps as above to the enemy object – but set up the enemy’s maxHealth to be smaller than 10, for example make it 5. This will make the enemy easier to destroy. You can set up more difficult enemies and boss level enemies in other rooms later that are more difficult to destroy.

Set up the enemy health

Add a Create event to the enemy object with the following code:

maxHealth = 2; //increase this to make the enemy harder to destroy
currentHealth = maxHealth;

Collision Event with Player object

On your enemy object, add a collision event with the player object. Add the following code to it:

instance_destroy(); //destroy the enemy object when it hits the player

Collision Event with Player Laser or Bullet object

On your enemy object, add a collision event with the player laser object.

currentHealth -= 1; //take some health off the enemy

with(other){
instance_destroy(); //destroy the player's laser when it touches the enemy
}

if(currentHealth<=0){ //destroy the enemy when its health reaches 0
instance_destroy();
}

To learn more about the with() method watch this video:

Code, Create, Play

Learning GameMaker?

Code Create Play takes you through building a complete game step by step over seven days.