Instantiating and Destroying Game-Objects in Unity
Objective:
To create a shooting mechanic where pressing the “Space” key fires a projectile.
In order to complete the objective we have to do the following,
1) Check if the “Space” key is pressed
2) When pressed, instantiate the Laser game object
3) Ensure the Laser game object travels upwards
4) Destroy the Laser game object once it’s off-screen
1) if Statements and fetching User Inputs:
“if” statements are conditional statements that tell the system what to do. They execute a block of code when the given condition is satisfied(condition = True).
“if” statement syntax is as follows,
We now need to be able to fetch Users keyboard inputs, this can be achieved by,
GetKeyDown returns true during the frame where the user starts pressing down on the desired key.
So putting it all together we get the following,
The next phase deals with the spawning a Laser game object when the Space key is pressed.
2) Instantiate:
Before we start spawning Lasers, we first have to create a Prefab for the Laser (Click here to know more about Prefabs).
With this done, we can start working on spawning the game object. This can be done by using,
There are many ways to Instantiate an object you can read more about it here, I’m going to use the following,
Here,
Prefab, the first parameter deals with the object that we wish to spawn or make a copy off.
Position, the second parameter deals with the position of where we wish to spawn the object.
Rotation, the third parameter deals with the orientation of the spawned object.
First, we have to create a serialized field so that we can pass the desired prefab through the editor.
Second, we have to find a position at which we want to spawn the desired prefab.
Here, assuming that your Player game object’s position is at (0, 0, 0). I want my Laser prefab to spawn right above my Player game object and to do so an ideal position happens to be at (0, 0.85, 0), that is 0.85 units above my Player game objects position.
Finally, we have to give a rotation to our Instantiated object. For this I want it to have no rotation/ default rotation. This can be done by
So with this, instantiation of Laser will look like the following
3) Laser Movement:
We managed to instantiate the Laser prefab, but it doesn’t function the way we intended it to, as we wished for it to travel upwards as we fire the Laser. That happens because we have yet to create such behavior for the Laser. Before we progress any further, it is important to know that every game object that contains a behavior, needs to be coded in its script. For example, the Player game object has a Player script that contains its movements and firing a projectile behavior. Similarly, the Laser prefab is expected to have its Laser Script which will contain its upward movement behavior.
Now all we have to do is add Vertical movement this can be done by,
To learn more on transform click here.
We want the Laser to move at a particular speed which could be altered from the editor to match the designers vision, this can be done by,
Now to make it move upwards all we have to do is,
The output of the changes made above looks like the following
There’s one issue with the above implementation. For each click of the space button there is one laser being instantiated which travels upwards. As the laser travels up and out of the scene the instance is still in memory which is redundant and memory intensive.
4) Destroy:
One solution to the problem generated above is to destroy the game object that leaves the scene. This can be done by,
“Destroy()” can take two parameters, the first being the game object you wish to destroy and the second being the time after which the object will be destroyed. We won’t be using the second parameter. Instead, we will use the position of the laser instance to destroy the object.
The final build should look like the following,