Get Your Personalized Game Dev Plan Tailored tips, tools, and next steps - just for you.

Unity: Get Distance Between Two Objects

Posted by Gemma Ellison
./
September 6, 2023
The cover for Unity: Get Distance Between Two Objects

Introduction

I worked with my colleague yesterday to teach him how to calculate the distance between two points in a Visual Effect Graph. I dug around for some helpful resources online, but I found little. I wanted to make a comprehensive primer on how to get the distance between two objects in Unity.

In the world of game development, precision and accuracy are paramount. One crucial aspect is understanding how to measure the distance between objects in your Unity project. It doesn’t matter what type of game you’re making: a first-person shooter, a platformer, or a racing game. For any game, knowing the exact distance between objects is essential for various gameplay mechanics.

In this article, I will explain how to get the distance between two objects in Unity. I will explore different methods for getting the distance between two objects. You will learn C# Math C# Physics.Raycast, Visual Effect Graph, and Shader Graph.

This is a series on Unity Physics. We recommend reading the series in order.

  1. Unity Physics
  2. Unity Collision Detection
  3. Character Controller and Input Systems
  4. Rigidbody Mass in Unity
  5. How to add Friction to a Rigidbody
  6. Cylinder Collider in Unity
  7. Box Collider in Unity
  8. Box Cast in Unity
  9. Sorting Layers
  10. Get the distance between two objects
  11. How to normalize a vector

Introduction to Distance Measurement in Unity

Unity is a powerful game development engine. As such, it provides various tools and methods for measuring distances between objects. This measurement is crucial for enemy AI, object interaction, and collision detection tasks.

The most basic tool is the Vector3.Distance method. A more advanced technique is to calculate the distance yourself. You can also use the Physics ray cast system to test the physics distance.

Vector2.Distance, Vector3.Distance, and Vector4.Distance Declarations

Unity declared these static methods for you in the Vector2, Vector3, and Vector4 classes.

public static float Distance(Vector2 a, Vector2 b);
public static float Distance(Vector3 a, Vector3 b);
public static float Distance(Vector4 a, Vector4 b);

Vector3.Distance Usage

Most often, you will use Vector3. The same basic principles apply to all distance methods. The method expects two inputs, a and b. Each Vector parameter should be of the same Vector type.

Here’s how to use Vector3.Distance:

Vector3 a = Vector3.zero;
Vector3 b = Vector3.right;
float distance = Vector3.Distance(a, b);
Debug.Log(distance);

The Vector3 Class

Before we delve into distance measurement, let’s understand the Vector3 class in Unity. Vector3 represents a point or direction in 3D space. Developers use Vector3s for object positions.

Methods for Calculating Distance

Measure the Distance Between Two Objects Using Vector3.Distance()

The easiest way to measure the distance between two objects in Unity is with the Vector3.Distance() method. It calculates the Euclidean Distance between two points: the straight-line distance.

Get the Distance Using Math

You can manually calculate the distance between two objects using their positions. This approach gives you more control and customization. For example, you can apply a custom distance formula.

Create a free account, or log in.

Gain access to free articles, game development tools, and game assets.