Hey there,
Is there a way to show a text mesh added to an object only when you within a certain distance from the object? At the moment it is being shown the from all distances.
I do not know how C# works so I prefer to be able to do it without using any code.
Thanks!
Cynikal
2
You can’t do that without code. Anything that requires any form of logic or thought requires code.
Unity isn’t a drag and drop game maker. Sorry.
You can look into one of the MANY Visual Scripting assets on the Asset Store.
However, i’ll help you out.
On your Text Mesh, make a script. Name it: “VisibleByDistance”
Then, open it and paste this code:
using UnityEngine;
public class VisibleByDistance : MonoBehaviour
{
public Transform PlayerObject;
public float DistanceToView = 10f;
private TextMesh myTextMesh;
private string MyOriginalText;
void Start()
{
myTextMesh = GetComponent<TextMesh>();
MyOriginalText = myTextMesh.text;
}
void Update()
{
if (Vector3.Distance(PlayerObject.position, transform.position) <= DistanceToView)
{
myTextMesh.text = MyOriginalText;
}
else
{
myTextMesh.text = "";
}
}
}
Now, that’s an EXTREMELY sloppy way of doing it, but it will work. Once you place that script on your TextMesh gameobject, be sure to drag/drop your player object into the variable of “PlayerObject” in the new script in the inspector. And play around with the Distance slider.