Welcome back to the continuation of our space shooter in Unity. We talked last time about firing cool downs but never really got into how we fire our ammunition or lasers in the first place. Let’s break that down by using a technique in Unity called “instantiation.”

What it means to “instantiate” a game object in Unity is to use code to clone a game object instantly in your game. You can find more in the Unity game docs here. As you can see, there are a lot of options in the function’s parameters.

Let’s do this with say a 3D capsule and make it smaller than the player and set it to a 0 origin position. We can apply a texture material and then make it a Prefab.

What is a Prefab you may ask? This is a key feature in Unity that allows you to make a game object template you can use over and over again. So like for lasers, this could be very helpful as you will see.

To make a prefab, just drag and drop the game object from the Hierarchy into one of your folders in your Project window. You will notice the object’s icon turns blue. Now, anytime you reference this Prefab, it will behave/look the same as the one from your Project folder.

Note: You can safely delete this laser Prefab knowing that if you need it again in your scene, you can just drag and drop it back into the Hierarchy.

Now for the code part. Since we are making the Player fire lasers, let’s open up the Player.cs script. We’ll need to add a private “GameObject” variable type to hold the value of our new laser Prefab called _laserPrefab:

Now add the following in the Update(); section to instantiate it:

We can save, go back to Unity, and see now that the Player script has a slot for us to add the laser Prefab. Cool huh? Let’s add it to the Player game object and hit play to see what happens…

So we’re getting the lasers game objects to instantiate but they don’t really go up do they? Let’s add some forward movement to the laser. To do this, we actually need to create a new Laser.cs script to add forward movement upon the Laser Prefab’s instantiation moment.

When you make the Laser.cs script, drag an drop it to the Laser prefab and open up the code. Let’s add a private float variable of 8 units per meter for speed:

And then add the up-translation of the game object upon every game Update over time (or AKA upon every instantiation occurrence):

Hit save and go back to Unity to see how it moves…

Great work! Now take note, these Prefabs are cloning in the scene at an alarming rate. If we don’t delete these over time, it may become a processing issue later. Therefore, let’s destroy them after they run off screen. You can find more about the Destroy method here.

Head back into the Laser script and add an if statement to check if the laser moves offscreen (+8 meters in the y-position). If true, destroy this game object:

Hit save, go back to Unity and hit Play. You should now see less Laser Prefabs populate your Hierarchy over time.

We’re almost done but we need some code cleanup. In the Player.cs script, we were able to instantiate the laser but we should make a new “FireLaser” function now so later if we need to debug an issue, it will be easier to tell where it’s coming from.

  1. Move the instantiate code from the Update function into a new function call “FireLaser.”
  2. Keep the key Input if-statement from the Update function. Just add the “FireLaser();” function here instead.
  3. Lastly, in the FireLaser function, have the laser start a bit above the player so it doesn’t just start inside the player by adding a bit to the y-position.

You can save and hit play in Unity. You should not see much of a difference but the backend code is now much cleaner.

We can also add in the fire rate and cooldown features back in here. If you need a refresher, here’s the setup for that article.

Thanks for stopping by! Looking forward to seeing you in the next article post.

Passionate animator and game developer; Wesley is a co-founder for Multivarious Games and a Creative Director in elearning at Xcelerate Media.