Homing Laser Unity 2D .1

Thomas Mauro
4 min readJun 8, 2022

--

I have been at work getting new things working in the Galaxy shooter. One of those is the homing or seeking laser or better known as a missile. I tried about 5 to 6 different ways all that failed until I was able to get it working. Now it works perfectly and adds way more fun and dynamics to the game. Here is a link to play it in a browser.

As I said I went with a homing laser so I used a laser sprite I had in my project. I put that together similar to the other laser-type powerups like a triple shot and booster shot.

My homing laser prefab with components. I put a sound, trial renderer, and particle effect to bring it more to life.

Now on to some code:

  • [SerializeField] private GameObject _playerHomingLaserPrefab; (Variable)
  • [SerializeField] private int _homingLaserCount = 0;
  • [SerializeField] private bool _isPlayerHomingLaserActive = false;
Player Script Variables

I went with the M key to launch the homing laser because it felt like the right spot. The N key would not be bad either. In the Update method, I kept the original laser method and added the new method for the homing laser. Plus this will check to see if there is any homing laser ammo to use.

Player script update for homing laser method

In the Player.cs a new method to call for the homing laser to be fired was created. private void FireHominglaser(). Give it a bit of offset and update the UI text to reflect the amount of ammo. Show below.

Player.cs

Next in the Enemy.cs just like for the laser I needed to do something similar for the homing laser. Therefore I put it as an if statement inside the existing code. Shown below. This will

Enemy.cs

Now back to the player.cs I make another method for PlayerHomingLaser() This method is called from the powerup.cs whenever the homing laser powerup is activated/picked up. With every homing laser powerup, you get you gain 4 and up to 24 total. Also used UpdateHomingLaserCount() to call in the UI Manager to update the variable (_homingLaserCount) and UI text. Shown below.

Player.cs

Now for the homing laser activation method. Needed to tell it to go from false to true. From not having homing lasers to having them. Plus then set up the cod in the UIManager.cs for the UI on the screen to work and update accordingly. When has ammo it will be green when at zero will be red.

UIManager.cs required variable
UIManager.cs update the homing laser amount the player has. Helpful info about ToString
Went with Text in text.
Gain 4 after collected

Drag the homing laser UI text into the new slot for it on the Canvas UI as shown below.

There is more to this and I decided I will make 1–2 more parts to finish this homing laser piece. Between building this game, learning, working full time, and taking care of my home/life I feel it to be best that I break this one into more articles. Decompressing is required for the rest of my day today. Thank you for checking it out and have a good one.

--

--