Lasers! Pew Pew (┛◉Д◉)┛彡┻━┻
First thing we’re going to do is create a placeholder for our projectile. it’ll be a simple capsule with a position of 0,0,0 and scale of 0.2 on its X,Y,Z axis.

Every time I push space, this should be shooting out in front of my player. Since this is going to be a consistently used object, its best to create a prefab out of it.
we’ll rename the capsule as laser, create a folder called,’ Prefabs’, and drop the game object into that folder to create a laser prefab. Lastly

We’ll also be creating a special material for the laser called,’Laser_Mat’ within our materials folder and apply that material to our laser game object. Lastly, we add a rigidbody to our laser for later use.

Now, lets get that firing script going…
Firing Script
First I declare a public game object variable in my player script and call it,’LaserPrefab’. Its set to private since there wont be anything else accessing the variable besides the player.

Then I drag my Laser Prefab(from the project view) into that variable via drag n drop within the inspector.

I create a Fire() Function with an if statement that checks for space bar input. When Space is pushed, it'll will spawn a project or Instantiate() it!.

Instantiate takes three parameters. it needs a game object which will be spawned when executed, a vector3 for where given game object will spawn, and rotation.
Very straight forward, we put in our Laserprefab for our gameobject parameter, set the vector3 to the attached transform’s position for now( will tweak later), and leave the rotation alone by assigning it quaternion.identity ( 0 rotation). Once these are filled, our script will be able to create ,’Instances’ our of game objects / spawn our bullets in game.
lastly we make sure we put our Fire method in update so unity can check if we are pushing space ( which executes it per amount of frames/sec you have while playing).

now we head back into unity and check out the progress.

yep yep, its spawning the projectiles as intended, but it isn't doing what lasers should be doing. lets change that.
Laser Movement
All we need to do is create a create a separate script for our projectile and attach it onto our prefab. The script will be named,’LaserBehavior’ and its purpose is continuously translate our object forward in the Y direction. Giving the illusion of the laser traveling through space.

all we have here is a private,’Speed’ variable to adjust the traveling speed of our projectile and a transform.Translate in Update() to move our projectile. Now we should have some pellets flying out from out player cube!


The pellets are shooting out as intended, but they’re creating quite a mess. They’re piling up in the inspector and in game because there's nothing to remove them. This is going to cause some severe performance issues over time.
All we need to do is check if our Gameobject has gone beyond our play space and then call the Destroy() function on the attached gameobject.

(0,8) is where the projectile appears off screen. So i made a if statement that checks if the game object is greater than (0,8). If its true, then the Destroy() function will be called and will destroy whatever game object is in its parameter. In this case, the attached game object. Now, it cleans itself up.
