Creating a Laser and Instantiating with Code!

We got our Player moving. They have boundaries, and can screen wrap. Now it’s time to give them a weapon — a classic laser beam! (Or capsule in this case)

Creating the Laser

First thing first, we have to create our laser capsule! Right-click in the Scene Hierarchy > 3D Object > Capsule. Reset the transform, rename it appropriately, and fix up the scale.

Creating our Laser Capsule!

Next we’ll create a Materials folder, and also a Red material to attach to our Laser so that it’s discernable compared to our Player.

Last part gets cut-off, but a Red Material is created and attached to the Laser.

Now the basics of the Laser are made, it’s time to make our first Prefab! Make a Prefabs folder and drag the Laser into the Folder in the Project Window.

Dragging any object from the Scene Hierarchy to the Project Window will make a Prefab Instance of that object. These Prefabs can then be instantiated, or created during run time, and will be exact copies of the Prefab.

They are extremely useful when editing Game Objects. Instead of tweaking every ‘tree’ for example, you could edit the ‘tree’ prefab and it will be applied to every instance of that Prefab!

The Laser GameObject is dragged into the Project Window, turning it into a Prefab!

Creating the Shoot function

Now that we have SOMETHING to shoot, we have to tell our Player to shoot/fire said Laser!

First let’s test if our Input controls are working. We’ll be using the Space bar to fire our laser, so let’s check to see that our Space key is working properly. Inside the Player script, inside of Update, we’ll type the following:

Test code inside of our Player script.

Checking to see if it works:

Check!

Instantiating the Laser

So Space key Input works. Now we have to tell our code to create (or instantiate) the laser when Space is pressed.

To instantiate an object, we need a reference of the Laser in the Player script. It is made [SerializedField] so that we can attach the Laser in the Inspector.

Creating a reference to the Laser in code.

Attaching the Laser Prefab to the Player Script:

Now they are linked and we can Instantiate the Laser. Inside our Space Key Input in the Player Script, we’ll type Instantiate(<GameObject>, <position>, <rotation>).

This Instantiates the Laser every time the Space key is pressed, at the position of the Player (because it’s in the Player Script), with the rotation of whatever the Prefab is set at (Quaternion.identity).

In action:

Sorry for the lag… I was having a big problem with my screen capture the day of writing this. We can create the Laser, but we have a big problem — they don’t move!

Laser Behaviour

As you can see in the example above, we have a major problem — the lasers aren’t moving at all! This is because we haven’t given the Laser’s any behaviour. There is no forces acting on them, and they don’t know to move, so let’s fix that!

Create a Laser script and attach it to the Prefab.

Now, let’s open up the Laser Script.

Inside the Update function, we’ll tell it to constantly move upwards, and multiply it by a speed variable we’ll create, and Time.deltaTime.

Now our Laser will constantly move up when an Instance of it is created!

Now the Lasers move:

Once again, sorry for the delay. But they move!

We have a couple other problems however:

  1. The Lasers that are instantiated are never destroyed and clog up our Hierarchy.
  2. The lasers are instantiated inside of our Player Cube. We will need to give them an Offset.

Destroying Lasers off-screen

Our current problem. Look at all those clones that we can’t even interact with anymore! It’s so messy, let’s clean it up.

Luckily, cleaning up the Hierarchy is pretty easy. Inside the Laser script, we just have to tell it to destroy itself if it’s Y value get’s above a certain Y value (off-screen).

In my case, I chose 8.2 as my out-of-bounds destroy threshold.

Creating a Laser Offset

Since the Lasers need to be above our Player, we have to edit the Y-value of where the Laser gets Instantiated.

We’ll create a laserYOffset variable, and inside out Shoot logic we’ll create a Vector3 laserOffset variable containing our Y offset.

Then we add the Vector3 laserOffset to the instantiated Vector3 transform.position.

The Lasers are now created above, and not inside, our Player!

The GIF delay will be fixed soon! Promise.

We can shoot lasers now! But we can also shoot as many as humanly possible… And I don’t think that’s very fair to the Enemies we’ll be shooting.

Next up we’ll create a handy Shooting Cooldown system to limit how fast the Player can shoot!

--

--

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
Frank Warman

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles