2D Game Development: Creating Projectiles

Thomas Kohut
3 min readJul 10, 2021

This article discusses the creation and implementation of projectiles.

Projectiles being fired from Player object

Projectiles play an important role in many games, whether it be used to attack enemies or obstruct the player. Today, we will be discussing how to create and implement player-based projectiles.

The first thing to be done is to choose an object to serve as a projectile. For this example, we will be using the capsule object. Once the object has been selected, it will need to be made into a prefabrication, or prefab for short. To create a prefab, just drag the object to be made into a prefab into the project section (it is general practice to create folder containing the prefabs).

Creating a prefab

Now that the prefab has been created, the laser object is no longer needed in the workspace and can be deleted. Next, we need to have the prefab spawn on the player’s input. To do this, we must use Instantiate, which allows for an object (in this case, our prefab) to be cloned. We must also designate the position in which the object is to be positioned upon creation as well as its rotation. For this case, we used Quaternion.identity to handle the object’s rotation, which just reflects the object’s original rotation. While the method to Instantiate does not need to be separated into its own method to work, it is recommended as it makes the code easier to read.

Now that creating the object has been sorted, we must allow the user to execute this method using a button. This can be done through using an if statement and the GetKeyDown method in the Input class, which checks for a certain key to be pressed (this can also be used for controllers, which will be discussed in a future article). In this case, the key that will be used is the space bar, which can be identified using the KeyCode. If the space key is pressed and the cooldown period is over, the Fire method will be executed and a prefab of the laser will be spawned in. This leaves us with one problem, how do we remove the prefab after it’s spawned in?

Instantiating the Prefab (_offset ensures the object does instantiate on top of the player object). _cd and _rof work in conjunction to handle cooldown so that the laser cannot be spammed.

To solve this, we must a create a script and attach a script to the prefab to handle its behavior. For this prefab, the Laser script handles its movement, speed, and destruction. The game object is destroyed when the object passes a certain position along the y axis, this case being the edge of the screen. This prevents the object from continuing on offscreen forever.

Script for the Laser Prefab. Handles movement and destruction of gameobject.

With that, our projectiles are complete and usable! But what if we wanted for them to interact with the player’s environment or collide with enemies? For that, we’ll need to focus on collisions and hitboxes, which is discussed here.