Dialogue box relative to object through camera

I have a static camera set up in which the character can move about the screen without the camera following. I also have GUI dialogue boxes set up but their placement seems to be based on a 0-800x, 0-300y region and therefore have no real relation to the character’s position through the camera’s eye. So the question is, how can I take a simple GUI dialogue box such as:

GUI.Box (Rect (20, 50, 110, 90), "Dialogue test");

and have it appear next to the character based on his position?

1 Answer

1

Choose a location on the character for instance, 2m above their feet:

  var pos = character.transform.position + Vector3.up * 2;

Convert it into screen coordinates:

   var screenPos = Camera.main.WorldToScreenPoint(pos);

This will work presuming you have a main camera tagged.

I can do that? Ah, cool. Hm... when I try to put the variable "pos" in place of the GUI box's position, I get an error: The type UnityEngine.rect does not have a visible constructor that matches the arguement list '(int, UnityEngie.Vector3, int, int)' GUI.Box (Rect (20, screenPos, 120, 90), "[Character name]"); EDIT: Oh, nevermind it seems I forgot to use "screenPos.y"

–