New Weapon — Homing Torpedo

Daniele Quero, PhD
3 min readAug 19, 2021

Objective: Implementing a new projectile which seeks for enemies

What I want to cover in this tutorial is essentially the logic behind the behaviour of this new power up. I am going to skip the tedious part about creation of prefabs (both collectable and projectile).

Our goal here is to create a homing torpedo, where by homing I mean capable of seeking the closest enemy, and pursue it. It was really a useful challenge, because many logics are involved and this makes your brain work hard to find a proper solution.

For example, how to find the closest enemy? How to travel towards him? How to behave during the flight?

Those are only some of the issues, and others come out if you want to let the enemies also shoot torpedoes (as did for lasers).

Let’s assume that game objects are ready to go with all their components (audio sources, sprites, animations, colliders…), so basically we have just to code. Before starting I will give just a glimpse of what I made:

As you can see, there is a Torpedo Script. There we are going to put all our logic. Let’s see.

In the Start() method we are going to put some initial features, such as setting the parent to laser container, grabbing other objects references and starting coroutines: one for an initial movement (right before pursuing enemies) and one for self-destruction if no enemy is killed.

The initial sprint coroutine provides an initial up movement: the torpedo will travel up, from the player, for a short time, after which begins the pursue.

This is a copy of what we did for other object destruction, but as a coroutine: after 5 seconds of life, if the torpedo is still in game (it did not collide with anything) we will end its miserable life.

Now we are going to look at the Update() method, the heart of this script.

First call we encounter is a method that removes far torpedoes, it’s basically the same as we did for lasers, but this time it is going to check all sides of screen since torpedoes doesn’t only fly up. Hint: I used a Rect object, look at my repo!

Then, if the initial sprint is over, the search for enemy begins: here I paved the way for an enemy usage in which the player is pursued, otherwise there is the search for the closest enemy ship.

This method is the flight itself: given a transform target, a direction is calculated and stored in a variable (initially set to be up direction). The translation occurs along this new direction at torpedo speed and will continue on the last direction stored if no enemy is in sight. Along the travel, the orientation of the torpedo is also changing using the Quaternion.LookRotation method which requires the forward direction and the up direction. The second is the one to be changed according with motion. Look at this method official documentation, it is very interesting.

Now, the core:

Simple: we look for all enemies in the container, evaluate their distance from the torpedo and select the smallest one.

Let’s look at the result:

The first clip is a successfully pursued and killed enemy.

In the second clip the enemy escapes and the torpedo runs out of time.

--

--

Daniele Quero, PhD

A professional developer with passion for game developing and skill-growing. A former Nuclear Physics Researcher who changed his life to pursue his dreams