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.
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.
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.
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.
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.
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.
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.