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 and .
abpublic static float Distance(Vector2 a, Vector2 b)
Parameters
Returns
Type | Description |
|---|---|
| float | The distance between the two positions, as an absolute (positive) value. |
Remarks
The distance between two objects is calculated as the magnitude of the resulting from vectorA − vectorB.
VectorVectorVectorB − vectorA gives the same result. In other words, is the same as .
Vector2.Distance(a,b)(a-b).magnitudeYou 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 .
GameObjectExamples
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 and .
abpublic static float Distance(in Vector2 a, in Vector2 b)
Parameters
Returns
Type | Description |
|---|---|
| float | The distance between the two positions, as an absolute (positive) value. |
Remarks
The distance between two objects is calculated as the magnitude of the resulting from vectorA − vectorB.
VectorVectorVectorB − vectorA gives the same result. In other words, is the same as .
Vector2.Distance(a,b)(a-b).magnitudeYou 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 .
GameObjectExamples
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}"); }}