Tutorial 5: Making your player shoot in GameMaker

In this tutorial we will step through how you setup your player object to shoot lazers or bullets in GameMaker Studio 2

Create the player’s lazer / bullet sprite

In the Assets Browser Resources menu (usually located on the right side of GameMaker studio 2)

  1. Right click Sprites
  2. Create a sprite called sprPlayerLazer
  3. Click the Import button and import your bullet or lazer GIF or PNG file player laser shooting
  4. Set the Origin of the sprite to Bottom Centre

player lazer shoot gamemaker studio

When we create the lazer object in the game, we need it to spawn from this point so it looks like it is coming from the player object.

Create the lazer / bullet object

In the Resources menu:

  1. Right click Object and click Create Object
  2. Name it objPlayerLazer and associate it with the sprite you create above
  3. Add a Create event to it with this code:
    //speed of the lazer, increase this number to make player shooting faster
    
    flySpeed = 10; 
    
    //change the value to make the lazer shoot faster
    
  4. Add a Step Step event to the lazer object and add this code to move the lazer:
    //minus from the y axis makes the lazer go up the room
    
    y-= flySpeed; 
    
    //change y-= to x+= to make the lazer shoot to the right
    

    The Lazer object needs to be destroyed when it leaves the room, otherwise it will still be active in memory and use up computer memory.

  5. Add an event to the Lazer object, this is in Other – Outside Room
  6. Change the comment code to Destroy self
  7. You want the lazer object to destroy itself when it leaves the room , so add this code to this Outside Room event:
    //destroy the lazer when it leaves the room
    
    instance_destroy();
    

  8. Now open the Player object , objPlayer
  9. Add an Event – Key Pressed for the letter Z
    //this creates an instance of the lazer on the players object's 
     //layer x and y co-ordinates of the //player object
    
    //the if statement only allows 5 lazers in the room at a time
    
    if(instance_number(objPlayerLazer)<=5){
    
    instance_create_layer(x,y, layer,objPlayerLazer);
    
    }
    
  10. In the Create event of the player object, objPlayer, add this line of code:
    //this sets the spacebar to whatever the Z key is doing
    keyboard_set_map(vk_space, ord("Z"));

Make player shoot with mouse button

Add a Step Step Event to the player object
Paste this code into the event:

//every frame check if there is a left button press
if mouse_check_button_pressed(mb_left)
   {
   
   //restrict the number of player lasers
  if(instance_number(objPlayerLazer)<=2){

  instance_create_layer(x,y, layer,objPlayerLazer);

}

   }

Spacebar down shoot with pause

If you want your player to shoot while holding down the spacebar, but have pauses in-between the shots, follow this.

  1. Open the player object, In the Create event setup the following variable:
    canShoot = true; //initialise canShoot to true
  2. If you setup a Z key event as described previously, delete it and remove the code for mapping the key in the create event, else ignore this step.
  3. Add Event to the player object, Key Down Space and use the following code:
    if canShoot = true  
    {
    instance_create_layer(x,y, layer,objPlayerLazer);
    
    canShoot =false;//when you fire a shot set canShoot to false
    alarm[0] = 10 ;//trigger alarm 0 after 10 frames 
    }
    
    
  4. Add Event, Alarm 0 to the player object with the following code:
    //when the alarm triggers set canShoot back to true
    canShoot = true;
    

Next Tutorial >> Adding Sounds to your game

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *