I use a distance check to display a hovering name/description. I used almost this exact script for items, signs, enemies, NPC.
void OnGUI()
{
if (my_cam == null) {
my_cam = GameObject.Find ("Main Camera").GetComponent();
} else {
float distance_from = Vector3.Distance(transform.position, _player.gameObject.transform.position);
if(distance_from <= sight_distance)
{
int label_w = 80;
int label_h = 40;
int label_offset_y = -60;
GUI.color = current_color;
Vector2 targetPos;
targetPos = Camera.main.WorldToScreenPoint (transform.position);
GUI.color = Color.black;
GUI.Label (new Rect (targetPos.x - (label_w / 2) +1 , (Screen.height - targetPos.y) + label_offset_y +1, label_w, label_h), item_name);
GUI.color = current_color;
GUI.Label (new Rect (targetPos.x - (label_w / 2), (Screen.height - targetPos.y) + label_offset_y, label_w, label_h), item_name);
}
}
}
↧