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.
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.
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!
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:
Checking to see if it works:
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.
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).
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!
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 the Lasers move:
We have a couple other problems however:
- The Lasers that are instantiated are never destroyed and clog up our Hierarchy.
- The lasers are instantiated inside of our Player Cube. We will need to give them an Offset.
Destroying Lasers off-screen
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).
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!
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!