0
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ What have you tried doing? Where are your exact problems? Finding the distance (in 3D space?)? Displaying it as text? As is you provide too little information for a good answer \$\endgroup\$ – UnholySheep Jan 16 '17 at 7:28
  • 1
    \$\begingroup\$ We could give you the code but first, you should do some research. There are lots of tutorials and articles for Unity. Try searching this and this. The first and/or the second match is perfect. \$\endgroup\$ – csisy Jan 16 '17 at 9:09
3
\$\begingroup\$

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();
    }    
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.