Creating a Homing Projectile

Let’s create a new weapon that will target the nearest enemy something like this:

Ok, so the plan is to

  1. create an array of everything tagged “Enemy” in the projectile (laser) script
  2. check the locations of each and identify the nearest one
  3. then adjust the course and rotation of the projectile to intercept.

Basically, FindClosestEnemy() and then TargetClosestEnemy() and we’ll put that in Start() like this:

And in Update, we’ll just need to continuously call the TargetClosestEnemy() so that if our target is destroyed, any projectiles in motion will find new targets.

Notice that we’re going to need a couple of variables to track the GameObject closestEnemy, and the projectile RigidBody rb which we set to our Rigidbody2D Component. Using Rigidbody2D is going to require some extra physics calculations, but will also give us an interesting chase effect.

Let’s look at FindClosestEnemy() first:

We foreach through a comparison of unordered array of game objects and store only the smallest of the comparisons. That becomes our target. We’ll just run this once from Start() to create our list of active targets.

Ok, now let’s look at TargetClosestEnemy(). We’ll run this once from Start() and then continuously from Update() so once an enemy target is destroyed, we’ll auto-retarget the next closest enemy:

Direction is easy, just subtract the projectile position from the closest enemy. Next we use the Vector2 variable since we are working with Rigidbody, and subtract our projectile position from the closestEnemy position. Then we use the z component of a Vector3.Cross to average out the rotation angle and combine that with a speed factor to get our angularVelocity. Now we just give the projectile a velocity in the up direction. This approach gives us a nice arc path instead of a direct point-to-point path.

To tweak the missile spin, we can just adjust the Angular Drag value on the Rigidbody2D until we get the result we want.




Unity and Flutter developer interested in creating mixed-reality applications.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Going Over Iterators … of Javascript

Polling Position of Animated Elements

Screenshot of TypeSabers typing game

Writing my first React App

Configuring a React app to handle Authentication without redux, with Hooks

Node Toolchain for Newbies: The Best Node Apps and Libraries to Increase Productivity

Java Script

Thinking in a JavaScript Way

JavaScript Code Snippet

How to Learn React Native Practically

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Richard Morgan

Richard Morgan

Unity and Flutter developer interested in creating mixed-reality applications.

More from Medium

Unity 2D vector movement with new input system

Getting Started with Unity

Pitfalls with Unity Unit Tests and [Timeout]

Easy Way to Make an Audio Slider