Road TO Dev — Firing my lasers!

Now that we have our prefab setup it’s time to have the laser do something and then have the player create those lasers on command!

Step 1: Having the laser move once it’s been created. This is pretty easy as we already learned how to make an object move when we started movement with our character. Inside the laser script.

Now it’s time to setup our player to fire the laser!

Step 2: We need to let our player know what object we will want to use.

A really easy way to tell the player what object to create is to make a wonderful variable you can drag and drop in the editor to assign the object. Remember to Serialize it so you can actually add our prefab to it.

Step 3: We will need to Instantiate the object. This is code for “create an instance of”. We will need to know what object to create, where to create it, and when to do so. Given this is a special operation on it’s own I recommend creating a method for Firing the laser to help keep update clean. Let’s have it fire when space is pressed.

Looking good so far, but remember earlier we set the lasers position where we wanted it to appear? Well we need to create that offset.

Of course now we can rapid fire as fast as we can mash the button, but that just seems silly. So let’s make sure there is a cooldown between shots. We need to set a rate of fire to keep to and of course a variable to keep track of time between shots.

Using this information and Time.time we can create a cooldown. We use canfire to store the time at fire plus our fire rate and then we can check when firing if the time passed since last press is greater than it was previously with the addition of our fire rate time.

Hi there! I am currently on the path to learn how to become a unity developer and will be documenting my journey as best as I can here. Thanks for looking!