GameMaker

GameMaker

73 ratings
How to add shooting system!
By Sofy
A simple system to add basic gunplay to your project! This guide includes: A simple easy to read format, Reloading, animation control and of course~ shooting!
7
2
   
Award
Favorite
Favorited
Unfavorite
Welcome
If you like this guide feel free to add suggestions for my next one ♥️

This guide is both assuming your brand new to gamemaker and or just looking for a quick easy to somewhat read guide!

if your new to gamemaker I highly recommend you type the code rather then copy paste
it helps more then you think
.gif]
Objects
For this guide I am assuming you have already have a player object with movement.
If you don't I already have a guide up just for this! click here


(feel free to use any prefix ect: obj_object, objObect, oObject)

To create your object, Go to your asset browser (by default should be on the far right of the window)

right click on the objects folder, Hover over create which should expand out a bunch of options. Then find and click object (at time of writing it should be the 5th option).


I'll be naming it oGun feel free to name it whatever you want!


create another object for your bullet! I'm naming mine oBullet
Sprites
Now were going to create our sprites!
To create a sprite, Go to your asset browser (by default should be on the far right of the window)

right click on the sprites folder, Hover over create which should expand out a bunch of options. Then find and click sprite.

3 sprites need to be created


  • sGun (should be a single idle sprite)


  • sGunReload (Your reload animation!)


  • sBullet (should be a single sprite)



Now we must add the sprites to the corresponding object!
  • Idle gun sprite goes to your gun object
  • Bullet sprite goes to the bullet object
make sure to set the sprites origin points to where the players "hand" should be
Events! (How they work)
Here I'm just going to talk about how to add events! feel free to scroll to the next header.

Before we can add some code were going to have to add events! I won't be going into depth about how events work but long story short, its how and when our code runs!

Double click on our player object in our asset browser. A window should open up in your workspace.

In the player object and within the events tab, right click and click add event
Add:
  • Create
  • Step

do not add a begin or end step.


now double click on the create event and lets get started!
Code (GUN)
Now for the fun part (atleast for me♥️)
before we start your window should look something like this


Create
For the create event, were going to add our variables and bools.
image_speed = 0 //set recoil (pixel) recoil = 0; //how many frames before I can shoot again cooldown = 5; //sets mag size magSize = 20 mag = magSize; //are we reloading? reloading = false;
Step
Now for the shooting part!
//set our keybinds var shoot_key = mouse_check_button_pressed(mb_left); var shoot_key_hold = mouse_check_button(mb_left); var reload_key = keyboard_check_pressed(ord("R")); //set x and y to player orign point adding recoil offset x = oPlayer.x - lengthdir_x(recoil, image_angle); y = oPlayer.y - lengthdir_y(recoil, image_angle); //slowly lerp recoil down recoil = lerp(recoil, 0, 0.1); cooldown--; //flips the gun correctly according to players xscale image_yscale = oPlayer.image_xscale; //rotate gun image_angle = point_direction(x, y, mouse_x, mouse_y); //checking for input and if able to fire if (shoot_key_hold && cooldown <= 0 && mag != 0 && reloading = false) { //create bullet while setting direction according to gun angle with (instance_create_depth(x, y, 0, oBullet)) { image_angle = other.image_angle; direction = other.image_angle; } //add recoil and reset cooldown recoil = 4; cooldown = 5; //remove a single bullet from mag mag--; } //check if were empty or player presses the reload key if (mag != magSize && reload_key or mag <= 0) { reloading = true; } if (reloading = true) { //set sprite + image speed sprite_index = sGunReload; image_speed = 1; //if sprite is on last frame reload gun and reset back to default settings if (image_index >= image_number -1) { //reset sprite sprite_index = sGun; image_index = 0; image_speed = 0; //set mag to its size and finish the reload mag = magSize; reloading = false; } }
Code (Bullet)
In the create event!
speed = 20; image_xscale *= speed;
Challenges
And were finished! Add the object to your room and press play and everything should be working!

Finished project with shooting, reloading and movement

Now I reccon your up for some challenges so try adding these features without my help 💖
(listed from easy - hard)
  • Play a sound effect when you shoot a bullet
  • Add some UI to show how many bullets are in your gun
  • Convert your code into a state machine

Final words
Thanks for reading! I hope this helped you in some way or form :)
I'm no teacher but seeing there was no proper guides I knew I had to try!

If you found this helpful please like and leave a rating!
If you need any help drop down a comment and I'll reply as soon as I can



Want to know how to do something? Drop a suggestion for a new guide!

.gif]
5 Comments
Zare-Mordor 17 Feb, 2024 @ 3:31pm 
Next a procedural terrain generation or chunking? :papyruswacky:
Sofy  [author] 24 Dec, 2023 @ 8:09pm 
no problem!
General Washington 24 Dec, 2023 @ 9:05am 
Oh ok, I see it now. Thanks for the guide!
Sofy  [author] 23 Dec, 2023 @ 8:53pm 
I did?
General Washington 23 Dec, 2023 @ 4:58pm 
Can you make a gun and player movement?