How to make a realistic functioning gun

So, I am currently working on a game where I would like it to include a gun in the view of first person. I would like it to have all basic options, such as; shooting, aiming, and reloading. Including animations where you can see the reloading and everything. As well as sound effects to go along with the weapon features. Possibly even particles like smoke that comes out of the barrel which I think I’d know how to do with maybe even a bit of a flame coming out of the barrel just like a real gun.

I have attempted to make a first person view for a gun and it works decent. All I have so far is the gun and when you left click it plays an animation that goes 0.01 seconds. Also, can you go lower than that? Because I haven’t seemed to be able to, because I have a good amount of frames as well to make the animation look smoother when it is shooting. But the problem is that I have to fully left click the mouse down to see the full animation and then it stays at the last frame until released the left click mouse button. It is pretty basic, and I have it fire at another sprite and it makes a bullet hole in those spots where the sprites are.

I really don’t know how I could make all of this work, maybe you could set a key to play a certain animation for the reloading, add in some sound effects when the shooting key or button is clicked, as well as the reloading key.

But I need help, could you guys possibly make an example? That would be so amazing. I’m sure it would also help many other people out there that are also wondering how to do something like this but aren’t sure.

Simply, I just want a fully functional gun that works like an actual one in real life and that also has the view in first person.

Again, all help is highly appreciated.

If you would like, I can also mention you in the game credits for this, saying something like "Gun System by: “Your name”
Plus early access to my game

Thank you for taking the time to read this post, and have a nice day. :slight_smile:

Well, in my opinion the best way to do is to keep every animation separated like so:


Change object animation as needed. To make sure animation is play and stop only when needed, use a variable to store actual animation number (0,1,2,3,4…) and change animation based on the value of this variable and also make sure if animation is finished (shoot, reload, aim) but specified key not pressed, play another (idle, empty) animation…
You can set animation and sound speed in the object editor, I don’t recommend to loop animation or sound as you don’t want to play any animation or sound forever, only if certain conditions are met.

Anyway, I just made a quick example for you. Not great, need to polish it but can be a good starting point: … sp=sharing
No aim, or smoke animation but you can simply add animations similar to shoot and reload. Smoke should be a separated object in my opinion.

The actual sounds and weapon animation I’m using in the example is ripped from a commercial game, so I don’t recommend to use them in your game (in case you would :smiley: ).
You don’t have to credit me in your game, I hope it helps :wink:

Thank you man, this really helps. :slight_smile:

With the aiming, do you think when a certain button or key is clicked then it should change the animation to look like it is aiming? Also, is there an option to actually make the gun look like it is zoomed in more? Maybe a camera of some kind?

I would use an animation for aim is well and yes, when a certain button is pressed, change animation to aim.
You can also zoom the camera using the Layers and cameras\Change camera zoom event. You can also center the camera on an object if you want. To aim weapon, I would try to change animation of weapon when aim, zoom camera a little bit when aim, and center camera on crosshair or weapon. If you find the weapon too small, maybe better option to scale size of the actual sprite object. You can scale inside the scene by selecting the gun and pull the little pixels on the side or you can also enter the size you want in the properties window. Or you can also scale using the Sprite\Size\Scale event.

Ok, thank you for all of your help.

So I got my own animations and everything working for the gun in first person going off of your example and I was wondering, what do you think is the best way to make it so when you click a certain sprite then it will show another sprite?

Because at the moment I have it so the shooting animation will only play if you left mouse click some transparent sprites. But the problem with this is that each of these are individual sprites and I want to make a lot of them. Also, for each sprite I put the very similar code you had in the example. I have also tried just simply making all of the events and actions for the whole pistol and how it works to be in a sub category to make it look like less. This way it wouldn’t be as overwhelming when trying to edit the scene. I want to make around 50 different places where I can shoot and then a sprite shows up and I think having so many individual sprites that look the same would be quite a bit, especially if you want to have other weapons with different sprites that are shown when clicking something. So I think there would simply be just a bunch of sprite objects.

Simply, what do you think would be the best way to make it so when you left mouse click a sprite for an example that looks like a box then it will create or show another different sprite? Then if you click anywhere else on that box sprite it will show another one for wherever you clicked. Very much like how shooting something with a gun in real life and seeing the holes it made.

Again, thanks for the help, it is highly appreciated. :slight_smile:

I don’t know what is your game concept, but in my opinion the gun should play shoot animation, every time you press left mouse button as long as you have ammo. To make a hole on the wall or something, simply check:

if mouse is on wall and left mouse is pressed and ammo >= 1 : create hole object at position of mouse.X() mouse.Y()

If there are other objects (targets) in front of the wall maybe better to check if mouse is not on target object

if mouse is not on target object and left mouse is pressed and ammo >= 1 : create hole object at position of mouse.X() mouse.Y()

To show an other sprite if you click on an objet, in my opinion the best way is animation. Change the animation. If you are using the same sprite object many places in the scene, to make sure only effected sprite object change animation, use an object variable. If mouse clicked on object, change value of object variable and change animation based on the object variable. You already have an example for this in my change animation example.

If you open the example and drop the object few more times in to the scene, only that object going to change animation which is clicked.
To change animation based on which weapon is used, I would use a scene variable to store that information. I think to change weapon, you have to press a key or have to click on an object. Simply, when you change the weapon object, set a new scene variable too, like so.
when pistol created Variable(equipped_weapon) = 1
when rifle created Variable(equipped_weapon) = 2

To change clicked object animation based on the weapon simply do something like this:

if mouse is on object and mouse is pressed and Variable(equipped_weapon) = 1 : change animation 1
if mouse is on object and mouse is pressed and Variable(equipped_weapon) = 2 : change animation 2

If you want to change animation based on where you clicked on an object that is tricky as you have to use little transparent sensor object over the object and change animation based on which sensor object is clicked or cut your object to small parts and use them as separated objects. If you put them together looks like it a whole object, but in reality it is built from small parts and change animation based on which part of the object is clicked.

Thanks man. :slight_smile:

I have some more questions though; from the example you made how can I make it so the sound for when the gun shoots only plays when I click a certain sprite and not just by simply left clicking anywhere? Also, how can I set the max clip size? For example I want 8 bullets instead of 10 at the most.

To play shoot sound only when you click on a certain sprite, simply add condition: the cursor is on object.
So the event looks like this:

the button left is pressed : play sound shoot
the cursor is on object

If you want to play shoot sound only if the object is playing a certain animation, store animation number in an object variable and add condition to test the value of this object variable too:

the button left is pressed                             : play sound shoot
the cursor is on object                                    
Variable animation of object is = something    

You can find the events that play shoot sound, under “play shoot sound” comment on the bottom.

To change the number of bullets, simply change the value of the ammo variable from 10 to 8 everywhere in the events. Just run through in my example if you see 10 anywhere, change it to 8. I guess you can use GD Search\Replace feature here to change it. Search for 10, Replace it with 8

Actually, could you just please update the example you made so whenever you click on a box and nowhere else it will create a sprite wherever the mouse is each time and they stay there? But then if you click a button they are all erased and if you select the option then it works as it did before.

You see, I am trying to make part of the game where you destroy an object so I would like to reset it so it looks like it has never been touched.

Be logical (only a bit of editing) :slight_smile:

First, create an object where the mouse is:

If mouse left click: ........Add Object_A in position X=MouseX("",0), Y=MouseY("",0)

But this will create an object every frame if the left click is pressed, we need a one-time reading condition, I’ll use a variable named Mouse_Pressed:

If mouse left click AND Variable(Mouse_Pressed) = 0: ........Add Object_A in position X=MouseX("",0), Y=MouseY("",0) ........Set Variable(Mouse_Pressed) = 1 If NOT mouse left click AND Variable(Mouse_Pressed) = 1: ........Set Variable(Mouse_Pressed) ) 0
So, when you click, add an object and switch the variable. If you keep pressed the left click, the game will not add more objects because the variable condition is not “satisfied”. If you release the left click, switch the variable again, then if you make a left click, the variable condition is fulfilled.

To delete every object when a key is pressed, just:

If key Your_Key is pressed: ........Delete Object_A
You don’t use any object condition (only a key condition), so every object is deleted (every Object_A fulfills an empty condition).

If you want to save every object before delete them (so then you can use a “Undo” button), you could save every object’s position in a file like in the example editor.gdg(?), then when you press the Undo button, create objects in each position saved in the file.

Thanks, I will try this out and let you know how it goes. :slight_smile:
Or if you would be willing to send me a link to the download of the example it would be wonderful as I could keep it and refer to it.

I am still trying to learn quite a bit, and I seem to get a bit confused on how variables work exactly. But with the help of you guys hopefully I will be able to understand them and how they are used through the examples you give me and whatnot.

So do you know how I could make a selection menu with different weapons work? For an example, I have a pistol and a rifle, and I want to change between the two by clicking on the buttons in the selection menu. When a weapon is selected have it so the other won’t be shown and each gun will truly work as its own.

I have updated my example
-you can select weapon
-weapon 1 got 8 bullets, weapon 2 got 10
-you can reset the scene
-shoot animation and sound play only if cursor is on object (wall) and as long as you have ammo
-every time you shoot, a hole (object) created in position of the cursor … sp=sharing

I don’t mind to help you out with some examples, but In my opinion if you don’t understand how variables works and how to use them, you should try to do small projects first. Your project gets way too complicated to be able to understand or learn anything from it with minimal understanding, especially from my badly designed examples. :unamused:
If you don’t understand exactly how my example works, (for example you don’t know where to change ammo in my example or you don’t know how to create, destroy objects, change animations in some cases, you don’t understand how do I change weapon in my example) I don’t recommend to go any further as it gets only more complicated. It is a very bad practice to try to achieve complicated things and hope that you going to learn as you go. Could work if you take small steps and you can understand everything as you go further, but if you don’t understand something, no point to go any further. Practice some simple things and work on small projects instead.

Only a suggestion. :wink:

Oh ok, thank you for updating the example and I haven’t really taken the time to much to learn a good amount about variables. I suppose I should but I felt like I didn’t really need to.

I’ve just noticed your PM. :unamused:

I have solved your glitches with your project, you can download the modified project file from here: … sp=sharing

To make sure the weapon play shoot animation only if hole is hidden, simply check in the condition if hole is hidden. Once hole is shown, the condition never runs again
To make sure the shoot sound play only if the weapon is shooting, simply check in the condition if weapon play shoot animation.
I’ve used mouse_pressed variable to force weapon to shoot when cursor is on hole, even if left mouse button is held but only if hole is hidden.

Thank you again, I appreciate your effort in helping me. :slight_smile:

Say, where did you learn about variables and how they worked? Also, what do you think would be the best source/website to check out to help me make a better understanding on them?

Well, I’ learned about variables in Pascal programming language. I’ve written simple programs in Pascal like calculator, unit converter and text based games. Pascal helped me a lot in early days to understand how to use variables.

The Variables works the same way every where.
You can add value, store information in variables, and you can make things happening based on the value of the variable, by changing the value of variables, you can affect how things going in you project. Simply like, If variable = 1 play animation 1, if variable = 2 play animation 2, life = 10, if enemy is shot then life = life - 1, if life = 0 destroy enemy. In GD there are different type of variables. Global, Scene, Object. Global variables used to store information globally, for example score. Scene variable is used to store information only in the present scene, for example player life, number of enemies in the scene. Object variables is used to store information for individual objects, for example life of enemy. If you have 100 enemies in the scene, you don’t have to use 100 different variables like enemy1, enemy2…etc to store life of every enemy, instead you can use the same variable for every enemy as an object variable. enemy.Variable(life) and this variable store life for every enemy individually.

From my experience (which is not much), the best way to learn how to use variables is practice with small projects. In my opinion very important to keep the project small and simple, take only small steps and concentrate on one thing at the time. For example, you want to make this FPS gun system. This can be very complicated so, I recommend to break them into parts.

  1. make the gun animation works perfectly if your press left mouse button, play shoot animation, if you press R, play reload animation as you need.
  2. make the sounds works perfectly, if you press left mouse button play shoot sound as you want, if you press R, play reload sound. Don’t bother with the weapon.
  3. make the target system works perfectly. object destroyed, object created, shown, hidden as you want, when you want. Don’t bother with weapon animation and sound.
    4.also need counters like score, ammo, life…etc. Work on counters, don’t bother with anything else. Make a counter that add +10
    to counter if left mouse button is pressed on object, make a counter that do -1 to counter if left mouse button is pressed and so on.

If everything works, put them together 1 by 1. Don’t do everything at once.
Put weapon and sound together first. If weapon works perfectly play animation and sound as you want, add target system.
If everything works, add counters, ammo counter to limit how many times you can shoot, add life counter to target to set how many times you have to shoot target to be destroyed…etc
Take as small steps as needed.

If you learned about variables before, only need to practice it with small projects, if you didn’t 4ian wrote a very nice tutorial here: … ariables?s

Thank you man, for the information and your advice. I think I will try to do some smaller projects before continuing so at least I understand a bit more of why things are what they are in the events and actions.

But, I was wondering, is it possible to make it so wherever the mouse hovers then the weapon will follow it in a way to make it more realistic? As in the weapon not staying in just one spot when shooting. I’ve tried doing some positions; but they didn’t really seem to work to well. Or whenever you left mouse click the object then it will change the position to near the hole that was created.

To check actual position of the mouse, every frame, you can use the MouseX(), MouseY() expression and you can change the position of the weapon, based on the position of the mouse. For example:

No conditions : X position of weapon is MouseX() + 200
No conditions : Y position of weapon is MouseY() + 200

Something like that, change MouseX() + 200, MouseY() + 200 as fit your project.
By using MouseX() and MouseY() expression, you can also move weapon based on the position of the mouse, by using All objects\Displacement events. If you want, you can also change the direction of weapon based on MouseX(), MouseY() expression by using Sprite\Direction events.
Or, you can also change animation of weapon based on the X,Y position of the mouse.