How to calculate the distance between two objects in Unity

Introduction

In this article we will see different ways to calculate the distance between two objects in Unity, this can be useful for example to know how far is the character from a certain point of the scenario or to activate mechanisms when the character is at a certain distance.

All the IMPORTANT information is summarized in the following TUTORIAL FROM MY YOUTUBE CHANNEL




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.

hierarchy of a project in unity 3d
Fig. 1: These are the GameObjects in the scene hierarchy.

assigned script that is in charge of calculating the distance in unity
Fig. 2: Script that will take care of the calculations.

The camera will have assigned the Script named “Distances” which is the one that will calculate the distance and write the values on the screen. As an extra, we’ll add a Line Renderer component to draw a line from one object to another.

In figure 2, we see that there are fields for both objects and for the distance indicator. We also have an enum that allows us to choose if we want to calculate the distance in space or projected in a certain plain.

distance between two gameobjects in unity
Fig. 3: This is how the scene looks with the objects.

Preparing the Script

The Script that solves the calculations calls “Distances” (complete Script at the end of the article), in the following figure, we can see the fields defined in this Script and the Start method.

fields and start method to calculate the distance between two positions in unity.
Fig. 4: Fields and Script Start method for calculating the distance between two GameObjects.

We have serialized fields (appear in the inspector) to assign the two spheres from the hierarchy, a TextMesh component for the distance indicator and the enum that allows us to choose which distance we want to measure.

update method that calculates the distance between two points in unity
Fig. 5: Code of the Update method.

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, for this, we define two methods “CalculateDistanceInSpace” and “CalculateDistanceXYPlane”, this last one takes into account only the X and Y components of the objects as if the objects were projected in the XY plane, this is useful when we use the orthographic view. In figure 6 we can see these methods.

Article about “Methods” in programming

methods for calculating distances in unity
Fig. 6: Methods for calculating distances in space and in the plane.



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” function of the Vector3 class is used and we use the positions of the objects as parameters, this method will return the distance between the two vectors.

Video 1: Distance between two objects in space.

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.

Video 2: Distance between two objects in the plane.

In the following video, we can see the difference between these two ways of calculating distances between objects in Unity.

Video 3: Comparison of the distance between two objects in the plane and in the space.

Conclusion

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 use, in this case, we used the XY plane, but there is also the XZ, the YZ plane and we can even define a plane oriented in any direction.

Appendix

Here you can see the complete script that calculates the distance between two objects in Unity.

script that calculates the distance between two vectors in unity
Fig. 7: Script Distances.

Scroll to Top
Secured By miniOrange