Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Distance

Returns the distance between a and b.
Read time 3 minutesLast updated 4 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

Distance(Vector2, Vector2)

Returns the distance between
a
and
b
.
public static float Distance(Vector2 a, Vector2 b)

Parameters

First position to compare distance from.

Second position to compare distance from.

Returns

Type

Description

floatThe distance between the two positions, as an absolute (positive) value.

Remarks

The distance between two
Vector
objects is calculated as the magnitude of the
Vector
resulting from vectorA − vectorB.
VectorB − vectorA gives the same result. In other words,
Vector2.Distance(a,b)
is the same as
(a-b).magnitude
.
You usually use this method is to find the distance between two entities.
For example, here an enemy will attack the player if they get within a certain range. The script below should be attached to an enemy
GameObject
.

Examples

using UnityEngine;public class DistanceExample_enemy : MonoBehaviour{ // Add a reference to the player, which is set in the Inspector window public GameObject player; // Set the distance at which the enemy attacks public float attackDistanceThreshold = 2f; void Update() { // Take the position of the player, and the position of this GameObject Vector2 playerPosition = player.transform.position; Vector2 myPosition = transform.position; // Use <see cref="UnityEngine.Vector2.Distance"></see> to obtain the distance between the player position and this GameObject's position float distance = Vector2.Distance(myPosition, playerPosition); // Check if the player is close enough if(distance < attackDistanceThreshold) { Attack(player); } } void Attack(GameObject target) { // Insert the attack logic here Debug.Log($"{name} attacks {target.name}"); }}

Distance(Vector2, Vector2)

Returns the distance between
a
and
b
.
public static float Distance(in Vector2 a, in Vector2 b)

Parameters

First position to compare distance from.

Second position to compare distance from.

Returns

Type

Description

floatThe distance between the two positions, as an absolute (positive) value.

Remarks

The distance between two
Vector
objects is calculated as the magnitude of the
Vector
resulting from vectorA − vectorB.
VectorB − vectorA gives the same result. In other words,
Vector2.Distance(a,b)
is the same as
(a-b).magnitude
.
You usually use this method is to find the distance between two entities.
For example, here an enemy will attack the player if they get within a certain range. The script below should be attached to an enemy
GameObject
.

Examples

using UnityEngine;public class DistanceExample_enemy : MonoBehaviour{ // Add a reference to the player, which is set in the Inspector window public GameObject player; // Set the distance at which the enemy attacks public float attackDistanceThreshold = 2f; void Update() { // Take the position of the player, and the position of this GameObject Vector2 playerPosition = player.transform.position; Vector2 myPosition = transform.position; // Use <see cref="UnityEngine.Vector2.Distance"></see> to obtain the distance between the player position and this GameObject's position float distance = Vector2.Distance(myPosition, playerPosition); // Check if the player is close enough if(distance < attackDistanceThreshold) { Attack(player); } } void Attack(GameObject target) { // Insert the attack logic here Debug.Log($"{name} attacks {target.name}"); }}