Take Your Time — Preventing Player Bullet Spam in Unity3D

Claudio Grassi
Mar 26 · 4 min read

Nothing can lead to boredom quicker than making a game too easy to play. If you’re making any kind of shooter and give the player the ability to create a literal wall of bullets then you will soon find them moving on to better gaming experiences.

Objective:

Limit the speed at which a player can fire projectiles.

Method:

Let’s start by taking a look at what a basic script might look like for shooting “bullets” from our player Object.

In this simple example, our Player class has two : a float called ‘Speed’ and a GameObject called ‘BulletPrefab’.
In our Update() Method we have some code to GameObject and we also look for player input in the form of the Spacebar. Once the Spacebar is pressed, we instantiate a BulletPrefab Object.

The problem is that in order to retrieve player input, we have to look for it in Update(). Update, as we know, can run upwards of 60 times per second or more — meaning that if our player is physically able to press the Spacebar that many times per second, they will shoot out as many bullets. So what to do, what to do?

Unity gives us a way of getting a value in seconds of how much time has passed from the start of the game. This value is constantly increasing in real-time. Let’s take advantage of it to limit the speed at which our player fires their weapon.

  • First we will to control this desired behavior.
  • The bool CanShootwill be the basis for controlling when the player is allowed to shoot.
  • We will control how often this can happen with the float FireRate variable.
  • Finally we will use our float NextShot to keep track of when we can set the player’s ability to shoot again to true .

In this bit of code we check to see if the value of Time.time is greater than the value of NextShot . If this condition returns true, then we can give the player the chance to shoot again by setting our bool CanShoot to true.

Once we have our bool in place, we can use it in our if statement where we look for player input as a second condition that must be met before allowing the player to instantiate a bullet.
When that happens you will note that we immediately reset our float NextShot to be equal to the sum of our float FireRate and the current value of Time.time . In this way we can slow down the rate at which the player can shoot bullets regardless of how fast they can press the Spacebar.

The final code looks like this:

Conclusion:

We now have full control of how fast the player can fire their weapon and we also have the ability of tweaking this value in our inspector to achieve the desired effect. By creating variables to hold all of these values we also allow ourselves the possibility of changing them at runtime through code thus creating a more modular and flexible system.

Much better!

CodeX

Everything connected with Tech & Code

Claudio Grassi

Written by

Experienced digital artist, Unity game developer & coder with a knack for problem solving and a passion for video games.

CodeX

CodeX

Everything connected with Tech & Code

Claudio Grassi

Written by

Experienced digital artist, Unity game developer & coder with a knack for problem solving and a passion for video games.

CodeX

CodeX

Everything connected with Tech & Code

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store