Making a Homing Projectile I: Art and Basic Setup

If you are here for the advanced homing missile coding secrets, stay tuned for my next article where I focus on the targeting, distance measuring and homing behavior. My objective for this article, is to create art for a homing projectile and it’s powerup counterpart, and then to get those objects working in my Unity game.

I already have a basic tusk projectile for the player, so I just color changed the same asset to make a homing missile version of the narwhal tusk.

I want it to have a light beacon that repeatedly flashes from the nose of it, so I made a sprite animation to enhance the projectile look a little bit. This is just using a circle shape outline with another circle shape with a gradient/ transparent fill, with a soft edge.

Now for the powerup. I use the letter H inside some circles with a few lines to finish the look. A gradient fill here, some glow there and I’m all done! It’s pretty simple, but I hope it’s a recognizable homing powerup set between some sights, even though it doesn't explicitly say “homing missile”.

Alright! It’s time to open Unity and import these exported PNGs as Sprites. I start by dragging the first sprite frame of the homing light animation into the scene view. I create a new animation, give it a title, hit record and then drag all of the animation sprites into the timeline. The spacing is all consistent other than the last almost transparent frame that hangs longer to give more time before the animation loop begins again.

Here is the homing light acting as a beacon for the projectile.

My player is has order in layer 1, the tusk on 2, and the homing light on 3 so it will appear in front of the others. I then drag it into the prefabs folder to make a prefab out of it.

On to the homing tusk. After I bring it into the scene I move the light object so that it sits at the tip of the tusk.

The homing tusk gets an appropriate name, as well as the tag of Tusk. This tag is what will trigger collisions on the enemy script, being the regular tusk projectile already works in this fashion. The layer ordering is set to 2, and the homing tusk gets components of types Polygon Collider 2D, Rigidbody 2D and the shared projectile script (Tusk).

Once this is done, I drag the homing light prefab into the homing tusk game object to make the light a child of the projectile. I then drag the homing tusk into the prefabs folder to make a new homing prefab!

The homing tusk powerup gets similar components with a Circle Collider 2D, a Rigidbody 2D and the shared powerup script. The tag is set to powerup, as well as the Layer (for Raycasting). This powerup gets a powerup ID of 7 in the inspector.

The circle collider received a small adjustment to fit the hard yellow outline.

Here is the homing powerup in the game view looking rather enhanced by the post processing layer.

As expected, the powerup is made into a prefab just like the other new game objects.

I select the Spawn Manager in the scene view, and then add a new element to the powerups array. The homing tusk prefab is now dragged into the new slot coming in at element 7.

The first thing I will need to fire the new weapon is a reference to the prefab on my player script. After saving this new variable, I return to Unity to drag and drop the homing tusk prefab into the player script component in the inspector to assign it.

The fire weapon method here in my player class is not showing a lot of other code that references the other projectiles, hence the random else if beginning statement. Two conditions need to be met for the homing tusk to fire .The homing tusk active bool needs to be true, as set by collecting the powerup, and the player needs to have more than zero ammo available. This is also nested in another statement that ensures the bubble blaster is false as well to enter. If all conditions are met, the homing tusk will instantiate and a sound clip will play.

The powerup will call this public method to start the homing tusk coroutine. I check to see if the triple tusk is active, and if it is I switch it to false. This way the player won’t be firing both special weapons at once if both powerups get collected. After the coroutine starts, the homing tusk is active for 5 seconds before switching back off.

I also update my triple tusk coroutine to check for the homing tusk and turn it off in the same fashion, if running when another powerup is collected.

On to the shared projectile script (tusk). For now, until my next article where I will address the actual homing behavior, I add the homing projectile ID of 5 to be a condition to use the regular tusk movement. This way it will behave in the same way as the regular tusk until I update the movement.

Here in the enemy script, you can see why adding the tag of Tusk to the homing tusk is what lets the enemies collide and destroy.

Now in the powerup script, I add a new case to my already existing powerup switch statement. When case 7 is called for the homing powerup, it will contact the player and trigger the public HomingTuskActive method.

In the Spawn Manager script, I have a coroutine running in a while loop for dropping rare powerups. I extend the range of the randomRarePowerup variable to include the new homing tusk. The max range of 8 is exclusive, so only 6 (bubble blaster) or 7 (homing tusk) will be called.

While there is no homing behavior yet, here is a clip of the player collecting the new powerup and firing the new projectile. I hope you join me in my next article where I implement the homing behavior. Thanks for reading!

I am an artist and musician, that is currently diving headfirst into game development with C# and Unity3D.