- Home /
Item name UI/GUI text over item/enemy in 3d
I have items that will drop from enemies and I want their names to appear over the top of them but i want them to be on the Gui or rather a canvas that is set to screen space overlay while the item itself will be in the 3d space. Im using worldtoscreenpoint to get the dropped items position and making a new vector 2. The problem I'm having is how do i set the text box to that items position so that as the player moves around the tag moves around on the GUI to stay over the item. I really like the way that Diablo 3 does this for dropped items if your looking for a reference to what im talking about. If you need a visual just gogole diablo 3 item drop and notice how all the items seem to be in 3d world space but the tags actually appear to be on the gui. Originally i was using texts on the items in 3d space and just having the box look at the camera (or in the opposite direction of the camera (givees a result closer to what I want) but I think the best was is to just have it on the Canvas. This same principal could be used for enemy health bars and I may use it for that as well so I included it to make the question broader and potentially more helpful to others. If you have a solution or a better way I greatly appreciate it. I have been searching around unity questions, the documentation, and youtube like how i usually find solutions to no avail. Thanks
Answer by Cherno · Jul 19, 2015 at 07:59 PM
I quickly tested this and it works; You keep the Text UI element as a child of the Canvas, of course, but you can just use a simple script to set it's position like this:
using UnityEngine;
using System.Collections;
public class FloatingName : MonoBehaviour {
//The item whose name should be shown
public Transform target;
// Update is called once per frame
void Update () {
if(target != null) {
Vector2 targetPos = Camera.main.WorldToScreenPoint(target.position);
transform.position = targetPos;
}
}
}
Your answer
Follow this Question
Related Questions
Changing just the name of person in UI Text 0 Answers
Why the text and the UI code is can't applying 0 Answers
4.6 UI Text rect does not expand automatically 2 Answers
uGUI letter spacing and kerning 1 Answer
Missing canvas elements on Build & Run. 2 Answers