Laser and firing— Day 4 of becoming a Game Developer

Yesterday we created our player, but now it’s time to start shooting stuff. For this project, we’ll be firing off lasers at enemies which we’ll cover in another article.

For the sake of prototyping our project first, we’ll be creating a capsule, and setting its scale to 0.2 on the x, y, and z-axis. I’ve then created a “Laser_mat” in the “Materials” folder as well as creating a “Laser” script in the “Scripts” folder. I’ve given the laser a greenish colour and assigned it to the laser in the “Hierarchy” as well as assigning the “Laser” script to the laser game object.

After I was satisfied with my laser game object I created a new folder under my “Assets” folder and called it “Prefabs”. A prefab is like a “master” object and you can clone that object many times over and each object will look and react exactly as the master object. Let’s say you want to place 10 doors in a building, does it make sense to set the door up 10 different times, or create it once as we want it to be and then clone it as many times as I need? The second option is an obvious choice here.

Pseudo-Code and its role in early development

Let’s discuss the logic in pseudo-code. You might be wondering what pseudo-code is, and I’ll briefly explain it.

When you want to code your logic, the most important step is to understand what you want to achieve. This is when you write pseudo-code. This is usually commented out using “//” before typing anything. And pseudo-code can look however you want it. In the case of our laser, we want to press a specific button to shoot a laser, and “document” what will happen when we press that button:

//press space button to shoot
//spawn new laser using instantiate
//destroy laser after a period or when it reaches a certain point

So now we have a basic understanding of what we want to achieve, now we can set out writing the code. After you’re satisfied with the logic and it does what you wanted it to do then you can remove the pseudo-code.

Pew! Pew! Pew!

I’ve now written my firing logic inside of my player script since the player will be shooting the laser.

I’ve also created a cooldown system to control how fast I can shoot the laser. We can implement it in a plethora of ways, but the simplest as explained in the Unity Docs is using the “Time.time” implementation. Time.time refers to how long the game has been running and we can now calculate how much time has passed between our two variables.

You may notice I’m referencing a variable called a “_laserPrefab”. Besides the 4 most common variables covered in my last article, Unity allows us to refer to Unity objects as well, and that “_laserPrefab” is just that. I have on my player a variable that’s a gameObject and that means I can actually attach a game object to that variable to spawn using the instantiate function which I won’t be covering in this article.

Laser be flying

I’m excited, my lasers are now firing when I press the space key, but they’re not flying up. The reason for that is because we haven’t specified that they should do anything. After all, the “Laser” script hasn’t been utilized yet and we have to tell it what to do.

Firstly, we need to deal with two issues. Firstly we need to get the laser flying up and secondly, we need to destroy the laser after a while because we don’t want to flood our game with hundreds of laser that’s in existence but we cannot see them anymore.

I’ve gone ahead and opened my “Laser” script and I'm going to start by getting it to move. I’m going to start by declaring a new float variable called speed and assign a value of 8.0f.

Next in the “void Update” function, I’ll be using a “transform.Translate” to move it up, and that’s done by using “Vector3.up” times by the speed I’ve declared and also times it by realtime which is “Time.deltaTime”.

And that wraps up our laser system. In this article, we’ve covered creating our laser prefab, assigning it a material and it’s own script, we’ve coded the logic to spawn the laser when we press a key and lastly, we made the laser move up and destroy itself when it leaves the screen bounds. You can view the full laser script here, and view the updated player script here.

Self-taught Unity and C# Developer with a passion for games and the stories they tell.