Unreal Gun System

This semester I have been building off of a previous project to remake a gun system similar to many of the first person shooters on the market today. I had several main goals with this project including:

  • versitile code that can be expanded upon to suit the developers/players needs
  • systems/tools that allow for easy adjustment with any coding necessary
  • easy integration into pre-existing projects

What Does This Mean For The Project?

To start with, I knew the code needed to be expandable into many different gun types. Most if not all first person shooters have multiple types of guns to allow the player to find a weapon that fits their playstyle. Without this the system would have very limited use cases.

Additionally, I want the system to require as little coding knowledge as possible. Being able to change things like the type of gun, rate of fire, burst size, and bullet spread should require little to no coding. This will increase accessibility and lower iteration times.

Finally, the code base needed to be standalone and work directly with then engine requiring no dependencies. The more work a developer needs to put in to integrate my system the less likely they are to want to use it.

Key Parts of The Project

Bullet Spread

Something that is very prevalent in many first person shooters is the recoil or spread of the bullets after prolonged firing. One notable example of this is CS:GO. Each gun has its own predefined bullet spread patterns forcing the player to react to it in order to maintain accuracy. While my project is different in that it uses phicically based bullets, I wanted to implement a similar system for bullet spread.

Here is an example of the final result:

Bullet Spread

In order to make this useful, it needed to be easy to adjust and it wass for that reason that I chose to use curves to define the bullet spread. In Unreal there is an edditor tool for defining curves which you can later interpolate upon to get a value. By allowing developers to use curves to define the vertical and horizontal spread, they can define where the bullets will fire at any point in time.

An example set of curves:

Curve Editor

Burst Fire Toggle

Implementing burst fire allows for the system to be taken in many different diretions. For example, you can implement a single shot weapon such as a pistol, sniper, or even a rocket launcher by using a busrt size of 1 or you can create a selective fire rifle*. Having all of these options in a single class greatly lowers the difficulty when creating a new set of weapons. Making a new gun is now as simple as changing a few parameters.

In the following gifs we can see the burst option at work. First we can see that the rifle is able to maintain the bullet spread through each set of bursts. This is important for guns like pistols where the burst may only be one bullet but needs to spread continuously as the player shoots. We can then see that it can be toggled. Behind the scenes, this is as simple as changing a boolean value. Lastly we can see that the size of the burst can be changed dynamically. For this demo I have it capped between 3 and 7 though this is not hard to change (a matter of 6 nodes in blueprint).

To accomplish this, a few changes needed to be made to the codebase. For starters, the logic behind deciding when the gun should be firing needed to be changed. If the gun is in burst mode, it should only fire if it has not finished the current burst.

if (burstMode)
{
    if (!burstComplete)
    {
        Fire(DeltaTime);
    }
        
}
else
{
    Fire(DeltaTime);
}

Additionally, if the burst has been completed, the player needs to release the trigger to reset.

if (burstComplete && !GetKeyState(RightFireKey))
{
    burstComplete = false;
}

Lastly, if the burst is over, the gun should stop shooting.

if (currentBurstCount >= burstSize)
{
    burstComplete = true;
}

Conclusion

Overall I really enjoyed working on this project. I have played so many games with smilar systems in the past, getting to make it for myself felt extremely fufilling. If you are interested in reading more into the code, the repo can be found here.

*Selective fire is the capability of a weapon to be adjusted to fire in semi-automatic, fully automatic, and/or burst mode