How to display distance between Player and object in UI text? Unity 5.5

by Alex   Last Updated May 15, 2019 10:13 AM - source

I am an beginner in Unity, with no experience in scripting. Can someone help me with a script to display distance between Player and object in UI text? Using Unity 5.5, the scene is quite simple just one room with the third person character moving around the object (CT scan). Thanks. Alex

Tags : unity 3d

Answers 1

As @csisy pointed out, it's better to make a few researches before asking a question. Especially for basic stuff like : computing a distance between two points and displaying a text on the screen.

I haven't tested the following code :

using UnityEngine ;
using UnityEngine.UI ;

public class DistanceVisualiser : MonoBehaviour
    // Drag & Drop the player gameobject using the inspector
    public Transform objectA ;

    // Drag & Drop the other object
    public Transform objectB ;

    // Drag & Drop the gameobject, child of a Canvas holding a Text component
    public Text UIText ;

    // Use LateUpdate to compute the distance **after** the player / object has moved
    private void LateUpdate()
        UIText.text = Vector3.Distance( objectA.position, objectB.position ).ToString();
January 16, 2017 09:15 AM

Related Questions

How to animate objects in Unity 3D

Updated July 12, 2015 13:05 PM