Creating A Projectile in Unity

Maurice Davis
3 min readFeb 6, 2023

When creating a laser, missile, or any type of projectile in unity, the first thing I like to do is use a capsule or cylinder by creating a 3d object and then change the item’s name.

Going in and zero the position and reduce the size to make the item look appropriate.

The next move is to create a new folder in the assets folder named prefab and then drag the new item into the prefab folder. When this is done, the item immediately becomes a prefab.

From here, I want to attach the laser to my player, and to do this, I want to create a private game object within a serializefield named laserprefab. Now I should be able to attach my prefab to the player.

After deleting the laser from the game, I can open my prefab folder, click on the player and drag my prefab laser into the new field attached to the player script.

Now that we have assigned the laser to the player, we want to trigger the prefab to appear when pressing the space bar using an if statement and Input.GetKeyDown to call on the Keycode properties.

Using KeyCode.Space, you are telling unity to activate this if statement when the space bar is down.

Adding Instantiate within the if statement tells Unity to clone whatever you list inside the function so that the first item will be the laserprefab. Next would be where to make it appear, transform.position tells Unity to make it appear where the player is at the time of the trigger. lastly would be Quaternion.identity, which refers to the rotation of the object, and this statement tells Unity to use the default rotation.

As you can see now, the projectiles are being created where the player is located

--

--