Creating Behaviors For A Laser

Kenny Pruitt
Unity Coder Corner
Published in
2 min readFeb 23, 2023

Objective: Adding Behaviors To Our Laser

To add a behaviors to our Laser we created, the first thing to do is create a new C# script for our Laser. First we want to define the Laser’s movement, to do so, we will do it similar to how we made the player move and define a laserSpeed, and in which direction the Laser is moving.

Don’t forget to assign a speed to your Laser in the inspector window and that you apply the change to your prefab of the laser.

Next you can test it out and your Laser should now be firing when pressing the space key.

As you can also see though, there is a new issue coming up. When you fire the laser the prefab is being spawned and shooting off, but it’s clogging up the hierarchy with all the objects. After the laser has left the screen the laser no longer needs to exist so we can Destroy it once it leaves the screen parameter.

Now the laser objects will be destroyed after exceeding the parameters.

--

--

Kenny Pruitt
Unity Coder Corner

Unity Game Developer, C# Developer, and Software Engineer