Distance between two Objects in Unity
In this article we will look at different ways to calculate the distance between two objects in Unity, this can be useful for example to know how far away the character is from a point of interest, or to activate mechanisms.
Setting up the scene
Let’s make a very simple scene that will have two spheres with 3D texts to indicate the number of the sphere and the distance between them.
The camera will have assigned the Script “Distances” which is the one that will do the distance calculations and write the values on screen. As an extra you will have a Line Renderer to draw a line from one object to another. There are fields for both objects and for the distance indicator. We also have an enum that allows us to choose what kind of distance we want to calculate.
The Script that is going to solve the calculations I have called Distances (complete Script at the end of the article), in the following figure the fields defined in this Script are shown together with the Start method.
We have fields to assign the two objects, a textmesh for the distance indicator, the enum that allows us to choose which distance we want to measure,
In the Start method we find the reference of the Line Renderer assigned to the GameObject camera.
In the Update method we are going to do the calculations, we can see next how it is programmed.
Finally we will have two methods that return the distance in space and in the XY plane, they are observed in the following figure.
Calculate distance between two objects in space – Vector3
Let’s start by looking at how to calculate the distance between two objects in space, i.e. considering the three dimensions of the scene.
The CalculateDistanceInSpace method in Figure 6 takes care of this. As you can see, the Distance method of the Vector3 class is used and as parameters we send the two three-dimensional vectors that we want to know at what distance they are, these vectors are the position of each object.
Calculate distance between two objects in the plane – Vector2
If we are working in 2D or if we are interested in knowing the distance between two objects projected on a plane, we can calculate this distance using only two dimensions of the scene.
The CalculateDistanceInXYPlane method in Figure 6 takes care of this. As you can see, the Distance method of the Vector2 class is used, two vectors are built using only the x and y components of the position of the objects and they are passed as parameters to the Distance method of Vector2.
In the following video we can see the difference between these two ways of calculating distances between objects in Unity.
We have seen how to use the classes Vector3 and Vector2 to calculate distances between objects in space and in the plane respectively.
In general Unity works in 3D space, therefore to measure distances in two dimensions it is necessary to project the points in some plane. We have to take into account that there are several planes that we can take into account, in this case we saw the XY plane, but we could use the XZ or YZ plane.
Here you can see the complete script that calculates the distance between two objects in Unity.