Instantiating & Destroying Gameobjects in Unity
Now that the player can move it’s time to make them shoot! To do this we need to spawn a projectile from the player on a key press.
First we make a gameobject to represent our projectile. For the prototyping stage we will use a capsule to represent this.
This code above spawns our projectile but only at the coordinates of 0, 0, 0. Obviously we want them to spawn with the player so…
Now we are getting somewhere! Lets make them move!
First a new script called Laser (shocking I know) added to the laser prefab.
transform.Translate command to make our projectile move in the vector3 direction of up.
But… we still have a problem. nothing is destroying these gameobjects when they go off screen.
As you can see the y position keeps going because nothing is stopping it. Lets fix that.
We use the Destroy command when the object is off screen.