- Home /
Question by
JohnTann · Jun 24, 2014 at 11:32 AM ·
guiworldtoscreenpoint
Fixed GUILabel with WorldToScreenPoint
Hi,
I'm trying to get a GUILabel to be fixed to a location using WorldToScreenPoint. So far it is ok only if I move in the x-axis, the label stays fixed. The moment I move in the y-axis the label goes bonkers and starts moving up and down with the camera!
using UnityEngine;
using System.Collections;
public class Test : 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(0, 0, 190, 50), "I DONT WANT TO MOVE");
GUI.EndGroup();
}
}
Comment