How to calculate the distance between 2 points and show it to the screen
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform other;
void Example()
{
if (other)
{
float dist = Vector3.Distance(other.position, transform.position);
print("Distance to other: " + dist);
}
}
}
To show it to the UI, you will need to have a text object in the scene and create a variable that updates its value.
The distance of two points can be calculated simply by reading the unity manual: Unity - Scripting API: Vector3.Distance
Here’s an example: (Don’t forget to include the “using UnityEngine.UI;”!)
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
public class DisplayDistance : MonoBehaviour {
public float Distance;
public Text DistanceText;
void Update(){
Distance = Vector3.Distance(other.position, transform.position);
DistanceText.text = "Distance: " + Distance.ToString();
}
}