[Unity] Get Closest Target

Featured Video Play Icon

Unity Get Closest Target

This will show you how to calculate what target game object is closest to your game object in both 2D and 3D.

We will be using a custom C# IComparer to sort an array based on distance from our given location. The result we will get is from closest to farthest away.

This can be used for:

  • Getting closest target for your AI to attack.
  • Letting bullets go through several targets in the right order.

Want realistic moving homing missiles for 2D or 3D? Check out these two assets.