“Funslingers” Devblog #29 | Making Homing Missile Behavior in Unity

Ethan Bristowe
Everdevs Community
Published in
3 min readOct 26, 2021

They say you shouldn’t bring a knife to a gunfight, but in this case the pen will be mightier than the sword… or maybe the knife will be mightier than the… gun?

Well whichever saying makes more sense here, we are gonna make a knife powerup that will be actually be the most powerful weapon in the game!

This knife’s powerup will have our most interesting and complex behavior yet! Once the player get’s the knife powerup, they’ll be able to throw knives that follow the enemies path to hit them. The knife powerup is designed to be a rare drop that can get the player out of a dire situation cause when it’s thrown, it will never miss it’s target!

Using our modular powerup system, we can easily set up our new powerup by simply creating our powerup prefab by attaching the powerup script to our Throwing Knife Icon GameObject, and then adding that powerup prefab to our Spawn Manager!

But that’s the easy part, now we dive into uncharted territory!

Let’s start this off by creating our KnifeProjectile script, where we can figure out how we want our throwing knives to behave.

Once the Knife Projectile is spawned, the first thing we would want to do is set things up like a normal projectile, getting reference to the rigidBody and starting the de-spawn timer.

The next thing we’ll need to do is get reference to all the enemies that exist in the scene, that way we can choose the closest one that hasn’t already been selected so that the Player can’t waste multiple knives on the same enemy.

Next, now that we have selected an enemy in the scene, we’ll use Vector3.MoveTowards in Fixed Update to track the enemy’s location while it closes in on the target!

Now, all we need to do is set up the player script to be able to spawn the Knife Projectile once they’ve collected the projectile.

We can use our other powerups as an example setting up the variables we’ll need.

We’ll add it to our list of powerups in the OnTriggerEnter() method to “Draw” the Knife once it’s picked up, as well as replace the shotgun powerup if it’s active.

And lastly, if the player shoots while the knife powerup is active, it will call the Fire() method inside the Knife script and operate just like the shotgun!

And with that, our homing throwing knife powerup is complete!

In the next devblog, we’re gonna give our guns some extra UMPH by adding some camera shake!

--

--

Ethan Bristowe
Everdevs Community

Passionate self taught game developer excited to learn and share everything I can about game development!