Made With Unity | 2D Space Shooter Part 3: Script Explanation And Shooting Laser
In this series of article / tutorials we are going to be making a small 2D Space Shooter using Unity3D and later on using the New Input System.
The Player Script
The Player script is a series of instruction that is allowing us to move the Player (Cube) using the WASD keys from the keyboard.
From line 7 to 10 we have some Variables that stores values. A Variables needs to have 3 requirement, a reference type(Private or Public), a data type (int, Vector3, float, bool, string, etc) and a name (_speed, _movement, _horizontalInput, etc).
private variables cannot be access by other script nor can be seen on the inspector, public variables are the opposite, they can be access by other script and been seen on the Inspector. If you want to see a private variable on the inspector you need to use the attribute [SerealizeField] like on line 7
On the Update() method we are calling the PlayerMovement() method that is doing the calculation to move the player on the game. Line 26 and 27 we are storing inside the Variables the value that are created when we press the WASD keys; Input.GetAxisRaw() is using the Input method from Unity, when adding “Horizontal” we are accessing the 0 and 1 when we press the A and D key Up and Down, “Vertical” is using W and S, Left and Right
Line 30 to 42 where moving the Player using the transform.Translate() on line 31 and on line 33 we use Mathf.Clamp() to not allow our player to go beyond the -3.8f y axis. Line 35 to 42 we are using a IF statement to change the player position if it goes out of bound of the screen.
Lets Do Some Shooting
Creating The Laser
Lets create a new 3D Object of type Capsule, named it Laser and adjust is Transform how it look on the image. It will look like a little Tic-tac 😅
Create a new Material inside the Materials folder and named it laser_mat and choose the color RED.
Select the Laser GameObject from the Hierarchy and drag the laser_mat material to the Materials section in the Inspector window.
Create a new folder called Prefabs
On the Hierarchy window select the Laser GameObject and drag it to the folder called Prefabs. You will notice that the Laser will turned blue, that a indication that you have created a prefab of the Laser object. You can do this regardless of the name of the folder but to keep everything organize it is nice to know what the folder is doing.
Create a new script and called it Laser, double click it and write the following code:
The method named MoveUpLaser(), the code inside is constantly moving the Laser object up to make it seen that we are shooting. The DestroyLaser() method is constantly checking if the laser has reach a y potion of 8f the Laser object will be Destroy() of the screen.
Add the Laser script to the Laser GameObject and press the Overrides button on the Inspector window and hit Apply All, delete the Laser GameObject from the Hierarchy.
Create a new Empty GameObject, named it [ — LASER CONTAINER — ] and add the Transform information from the image
Updating The Player Behavior
Double click the Player script and add the following lines of codes and save the changes:
The new ShootingLaser() method is checking if I can shoot when I press the Space key and a giving time has pass, if I can shoot I can call and clone (Instantiate()) the Laser prefab object where the player position is and store the new Lasers inside the Lasers Container to keep everything nice and organize.
Select the Player and add the Laser prefab (the one from the Prefabs folder) to the Laser Prefab field from the Player script, you can drag the Laser prefab or you can hit the dot where the mouse is on the image, go to the Asset section and select Laser. Do the same step for the [ — LASERS CONTAINER — ] GameObject to the Lasers Container field.
Press Play to test it out