How to Make a Basic Gun Functional In Unity

trenton hodge
5 min readSep 30, 2017

--

In Unity, it can be relatively difficult to get a gun working in Unity without spending a lot of money for assets that come with pre-made scripts; However, there are ways to take free assets, and turn them into functional weapons. First of Course, if you’re doing this for the purpose of a FPS game, you will need to create a first person character or import one from the standard assets package that comes with Unity.

Once you’ve done this, drag the FPSController Prefab into your hierarchy. It should then look like this.

Now that you’ve done this, you can either take a gun asset that you already have, and put it into the hierarchy (as a child of the camera in the FPSController) or import a package. In my case, I’m using the one shown below that also includes animations for the guns as well. For my game, I’ll be using the handgun prefab model.

The next step is to align the gun so that camera can see the gun as if the player is holding it. The positioning of the gun is up to you.

The Game View

The Above Picture is what it looks like from the player’s point of view. The Below is the scene view and is what it looks like from the editor’s point of view.

The Scene View

If you’re using the package that I’m using here, you will need to highlight the handgun in the hierarchy, and uncheck the “Play Automatically” box in the Animation. Otherwise, the gun will constantly act like its shooting without any sort of input.

Once you’ve done this, you are ready to start adding scripts to it. Create a script called GunScript in C# and open the editor. Copy, or change this script to fit your needs.

In the Above script, the two public variables on the top allow for you to change the damage and the range of the gun in the inspector when you have to the gun highlighted. The shoot function, in this case, tells the gun to play the given animation, play the given audio that it was assigned, and play the particle system that was assigned. If you do not have all of those components attached to your gun, you may want to delete or change some of this script. The Raycast mentioned in the script allows the gun to actually shoot things. Don’t forget to add this script to the gun in the hierarchy. Once you do, drag the camera for the FPS character into the public variable slot in the inspector called “Fps Cam” to make the reference so that the computer knows what the script is talking about. For any object in-game that you want the player to be able to shoot, add the following script to that object.

Being that this script is really basic, the health of the “enemy” in this case is 50, and the damage of the gun is set to 10 so it will take 5 shots to kill the enemy with this script. You can change this, but this is the default. After the “enemy” has been shot 5 times, it will simply disappear; Although, you can change that as well to play a death animation instead for the enemy. If you want to add audio, and a muzzleFlash to the gun when it’s fired, add an audio source component to the handgun, and a particle system of your choice as a child gameObject to the handgun as well. Once you’ve done this, change the names of the references in the GunScript, and it should work.

Your gun’s components should look something like this, and your hierarchy should have the the particle system for your gun listed as a child of the gun.

The Only thing that’s left now is to add the crosshair for your weapon. You can either create your own, or import a package that comes with one. To Add your crosshair to your game, first add a UI Image to your hierarchy.

Once you’ve done so, your game window should show a blank white square in the middle of the screen that looks like the below.

After this, you just need to add the image you wish to use for your crosshair. Simply highlight the image in your hierarchy and drag you desired image to the “Source Image” slot in the Image Component.

Once you’ve done that, you can change the color, and any other attributes to your liking.

--

--