My journey becoming a Unity game developer: Laser prototype for Galaxy Invaders pt.1

Rhett Haynes
4 min readJun 9, 2022

--

Objective: Creating a laser prefab object to serve as a prototype. When the space bar is pressed, the laser will be given behavior to move upward at a certain speed in actual real-time from the Player object.

Let’s add a Laser prototype to the Scene by creating a new 3D Capsule object name Laser. Scale the Transform on all 3 axes to 0.2 to make the Capsule object small enough to look like a laser coming from the Player object.

Laser prefab prototype created.

We need to add collision behavior to the Laser, but in order for collisions to happen both objects colliding need to have Collider components attached to them. Also, at least one object must have a Rigidbody attached to them as well which in this case makes it easy for us to add a Rigidbody component to the Laser since it’s a prefab and every Laser in the Scene will automatically have a Rigidbody attached to them making collisions with other objects easy.

With the Laser game object selected, click on Add Component and add the Rigidbody component. Turn off the Use Gravity because if we don’t, the lasers will fall downward automatically from the gravity applied.

Rigidbody added to the Laser prototype.

With the Laser component set, inside the PlayerBehavior script we will create a new variable Type GameObject name laserPrefab to assign the Laser game object to this variable for the Player object to use.

Inside the Update() function, we want to check IF the SPACEBAR key is pressed. If it was, we want to INSTANTIATE the laserPrefab at the Player’s position with no rotation. To make the Laser not rotate, we have to use the Identity method from the Quaternion class which Unity uses to handle rotations.

Laser prefab will instantiate when the space key is pressed.

Back in the Inspector, with the Player selected we need to add the Laser game object to the Laser Prefab slot in the PlayerBehavior script component. Play the game and notice the lasers instantiating onscreen, but not moving anywhere.

Lasers instantiating when the player presses the space key.

Create a new C# script name LaserBehavior in the Scripts folder and attach it to the Laser prefab. Next, create a new variable Type Float name speed with a default value of 8f. We will use the Private Access Modifier and the SerializeField attribute to control the security of this variable.

In the Update() function, move the Laser’s transform through the Translate() method. In the Translate() method move the Laser using the Vector3 UP as this will make the Laser move upward at 1 meter per frame.

Player’s laser set to move up 60 meters per seconds.

Notice the lasers moving upward real fast since Update() is called every frame which is about 60 frames per second.

Player’s lasers shooting at 1 meter per frame.

To make the Laser move in a more realistic way, we will multiply real-time from Unity’s Time class using deltaTime.

Player’s lasers set to move up in real time.

Notice the Lasers moving up 1 meter per second now.

Player’s lasers moving up 1 meter per second using real-time.

To make the Laser object move up more impressively, multiply the speed variable with the Vector3 Up method and the Time’s deltaTime.

Speed is multiplied with the lasers movement upward and actual real-time.

Lasers moving upward at 8 meters per second giving it a more effective look to shooting at potential enemies. Notice that every Laser instantiated in the Hierarchy is remaining active for as long as we keep playing the game.

Lasers moving upward at 8 meters per second.

To fix this problem, we want to destroy the lasers when they move off-screen. Move the Player object up until its off-screen and round off the Y-axis value to the number that has the object completely off-screen.

Showing where lasers need to be destroyed when moving off-screen.

In the Laser’s Update() function, we want to check IF the Laser’s transform position is above the screen which is 8 units for us on the Y-axis. If the condition is true, we will Destroy this game object. The Destroy method also comes with another option we could use which the amount of seconds to wait before destroying the game object, but we don’t need to use this option since we want the Lasers to be destroyed as soon as it reaches the unit amount we selected to represent the off-screen value.

Will destroy the laser game object when it reaches 8 units on the Y-axis.

Now the Lasers are being destroyed when they reach 8 units up the screen.

Lasers destroyed when they go off-screen at 8 units on the Y-axis.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet