- Home /
 
Keeping a label fixed with WorldToScreenPoint
Hi,
ok I'm just basically trying to have a GUI label fixed to a point in the world. So if I move around the label will go off screen or back on screen as I move around.
With the code below I can achieve this if I move in the x-axis the label is fixed, the moment I start moving in the y-axis the label goes bonkers and starts moving with the camera.
I dont really know what is going on =(
 using UnityEngine;
 using System.Collections;
 
 public class TestShit : MonoBehaviour 
 {
     float speed = 0.5f;
     Vector3 ScreenPos;
 
     void Update () 
     {
         float hori = Input.GetAxis("Horizontal");
 
         if(hori > 0)
         {
             transform.Translate(Vector3.right * speed);
         }
         else if(hori < 0)
         {
             transform.Translate(-Vector3.right * speed);
         }
 
 
         float vert = Input.GetAxis("Vertical");
 
         if (vert > 0)
         {
             transform.Translate(Vector3.up * speed);
         }
         else if(vert < 0)
         {
             transform.Translate(-Vector3.up * speed);
         }
 
         ScreenPos = Camera.main.WorldToScreenPoint(new Vector3(0, 0, 0));
     }
 
     void OnGUI()
     {
         GUI.BeginGroup(new Rect(ScreenPos.x, ScreenPos.y, 200, 100));
             GUI.Label(new Rect(10, 25, 190, 50), "I DONT WANT TO MOVE");
         GUI.EndGroup();
     }
 }
 
 
              I can not say if it is related without trying but you probably don't want to use ScreenPos like that. WorldToScreenPoint returns coordinates in screen space where (0,0) point is bottom-left of the screen. On GUI (0,0) is top-left of the screen. So change your line 39 with (That should solve something atleast):
 GUI.BeginGroup(new Rect(ScreenPos.x, Screen.height - ScreenPos.y, 200, 100));
 
                 Your answer
 
             Follow this Question
Related Questions
Converting world coordinates to screen coordinates 1 Answer
Wrong WorldToScreenPoint converting (not inverted y) 0 Answers
GUI Button Transform Screenspace 0 Answers
Fixed GUILabel with WorldToScreenPoint 0 Answers
Custom Cursor with Kinect Wrapper 1 Answer