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)
- Right click Sprites
- Create a sprite called sprPlayerLazer
- Click the Import button and import your bullet or lazer GIF or PNG file
- Set the Origin of the sprite to Bottom Centre
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:
- Right click Object and click Create Object
- Name it objPlayerLazer and associate it with the sprite you create above
- 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
- 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.
- Add an event to the Lazer object, this is in Other – Outside Room
- Change the comment code to Destroy self
- 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();
Now open the Player object , objPlayer- 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); }
- 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.
- Open the player object, In the Create event setup the following variable:
canShoot = true; //initialise canShoot to true
- 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.
- 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 }
- 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