Instantiating and destroying game objects
Now that we have finished up getting our movement set up for the player, lets get started some fire power. As this is a space shooter, what better way to build a shooting mechanism than with some lasers. However, as we are just working with basic objects for now as placeholders, we will just start with a 3d capsule to play the role of a laser. As well, as this will be a prefab, which is a component that allows us to save it in the project for reuse. Essentially anything that you see in games that are the same image repeated over and over, like bullets, lasers, walls, etc. are all prefabs. This allows developers to quickly implement their objects into the game without having to make new scripts and edits to it over and over again.
As you can see, we have created a new folder for our prefabs, along with linking our capsule object to the prefab and coloured it red. As well, we went ahead and created a laser script.
Now what we will do is test to make sure that we have our space key set up proper so that we can fire off our lasers. To check, we simply create our code that was used earlier for moving the player, but enter in space as our key press. From there, we will do a quick debug.log check to make sure it is working proper, and once it is, we can change it to our laser instead.
For Unity, the line of code we would write for creating a game object is Instantiate. Instantiate, for programming, means to create an instance of an object.
For the line of code, we see that there is Quaternion.Identity. What this means is the rotation of the object in a vector. When we add identity to the end of it, we are telling Unity that we do not want a rotation at all to the object.
Now that we have our lasers being created every time we press Space, we now have a few things that have to be figured out.
- How do we get our lasers moving upwards and
- How do we destroy our lasers once they leave our play screen, so that we don’t have them all travelling forever and overloading our game with objects.
First off, let’s make a code for our laser trajectory. As we already know how to create movement for an object, this is a simple issue to solve.
As we can see, all that we need to do is create our transform.translate code, and tell Unity that we want to move the laser upwards. From here, we can add a speed variable that we find is a good rate and voila, we have a moving laser.
Now, we have to find out how to clean up our hierarchy so that the lasers do not build up forever in there. First thing we have to do is figure out is where out laser goes off the screen.
What we do is start the game up and fire off one laser. We will just pause the game so that it stays in the game view and move it until we see the edge of it on the view. As it starts to disappear at about 6.9, we will go up to 8 on our y axis to delete is just to be safe, incase we have a longer visual on our lasers once we add the actual image to the object.
Now that we have our destruction point, we have to write our code for the laser to be destroyed.
Now we have figured out how to get our laser to move upwards, and how to destroy it once it reaches a certain point.
Finally, what we are going to do is create an offset for the spawn point of our laser so that it does not spawn in the middle of our player, but slightly in front of it. This will make the laser look cleaner when we instantiate the laser.
First, we will want to figure out a distance in front of our player to start spawning our laser from.
With our player set to (0, 0, 0) we will want to spawn out a laser and move it so that it is slightly above our player. From here, we will go to our player code and figure out a way to add the 0.8 onto the y axis spawn point. What we are able to do, is create a new vector 3 with a (0, 0.8, 0) setting.
That’s all we got for today. We’ve learned how to make a laser, adjust it’s speed, destroy it once it goes off screen and offset it’s spawn point so that it looks cleaner when it spawns. Next time, we will look at creating a cooldown type system for the laser so that it levels the playing field a little amongst all players, and a little bit of cleanup in our coding.