Closest object, Unity3D

Closest object, Unity3D

This post contains affiliate links, which means I may receive a small commission, at no cost to you, if you make a purchase through a link.

If you need to find the closest object to another, let’s see the closest enemy to the player avatar here are three different ways of doing it.

Scene setup

I have created a simple player that can walk around using the CharacterController in Unity3D.

I have also added a static floor and created a few cylinders with a rigid body on them and placed them in a layer I called Dynamic.

The last step is creating a script to place on your character, I created the GetClosestObject.cs script in which I created an enum to switch between the three different ways of getting the closest object. We are going to work with colliders and change the material on the closest one.

I perform a OverlapSphere to get the colliders within a radius and perform the algorithms on that array of colliders.

First way

We are going to do the traditional brute force approach, going through the list of colliders and see which one is closest.

We are going to create the method as an extension to the Transform class meaning we can call it directly on a transform instance.

 using UnityEngine; public static class TransformExtensions { public static Collider GetClosestCollider(this Transform transform, ref Collider[] colliders) { if (colliders.Length <= 0) { return null; } Collider closestCollider = null; float currentClosestDistance = float.MaxValue; foreach (var it in colliders) { float distance = Vector3.Distance(transform.position, it.transform.position); if (distance < currentClosestDistance) { currentClosestDistance = distance; closestCollider = it; } } return closestCollider; } }

Basically we compare each collider with eachother always updating which is currently the closest one, so when we have traversed the entire list we have the closest one.

Second way

The second way we willactually sort all the colliders from closest to furthest away by implementing our own comparer and use that to sort the array.

 using UnityEngine; using System.Collections; public class DistanceCompare : IComparer { private Transform compareTransform; public DistanceCompare(Transform transform) { compareTransform = transform; } public int Compare(object x, object y) { Collider colliderX = x as Collider; Collider colliderY = y as Collider; Vector3 offset = colliderX.transform.position – compareTransform.position; float xDistance = offset.sqrMagnitude; offset = colliderY.transform.position – compareTransform.position; float yDistance = offset.sqrMagnitude; // This is another way of doing it. //float distanceX = Vector3.Distance(colliderX.transform.position, compareTransform.position); //float distanceY = Vector3.Distance(colliderY.transform.position, compareTransform.position); //return distanceX.CompareTo(distanceY); return xDistance.CompareTo(yDistance); } } // To use it in a method Array.Sort(colliders, new DistanceCompare(transform)); Collider closest = colliders[0];

What we do is inherit from the IComparer interface, in the constructor we take in a transform, that’s the transform we want to compare the other objects to.

Then we implement the Compare method that takes two objects of type object as parameters. We convert those to colliders, keep in mind that we are not checking to see if the conversion was successful or not.

We then calculate the distance between our transform.position with both the x object and the y object and return the result. Which is either less than zero, zero or greater than zero.

Finally we use our comparer when sorting the array thus the first element in the array will be the closest one, and the last element will be the one furthest away.

Third way

For the third way we will use a k-d tree which is a structure for organizing positions in a space of k-dimensions.

I did not implement the tree myself but ditzel have created one for Unity3D which you can get here.

Using this tree we at the start of the game add all our objects we want to compare too to our tree, we then update the positions in the tree before asking it to get the closest object too us.

Do you want the entire project you can get it here.

Tags: , ,
Top