- Home /
Question by
superventure · Jul 02, 2013 at 09:25 AM ·
rotationguiscreentoworldpointhealth bar
ScreentoWorld point help- rotation
Hello. The health bar is not sticking to just the gameobject's position; it shows up again when the camera is rotated 360 degrees. What am I missing? Thank you
var screenPosition: Vector3 = Camera.main.WorldToScreenPoint(transform.position);
//draw the background
GUI.Box ( Rect (screenPosition.x , Screen.height - screenPosition.y - size.y , size.x, size.y), transform.root.name);
// draw the filled-in part:
GUI.Box ( Rect (screenPosition.x , Screen.height - screenPosition.y - size.y , size.x * barDisplay, size.y),"");
Comment
Where do you have this script? In an OnGUI function I assume?
Best Answer
Answer by robertbu · Jul 02, 2013 at 12:39 PM
Check the 'Z' coordinate of the return value Camera.main.WorldToScreenPoint(). If it is less than 0, the position is behind the camera.
if (screenPosition.z > 0.0) {
//draw the background
GUI.Box ( Rect (screenPosition.x , Screen.height - screenPosition.y - size.y , size.x, size.y), transform.root.name);
// draw the filled-in part:
GUI.Box ( Rect (screenPosition.x , Screen.height - screenPosition.y - size.y , size.x * barDisplay, size.y),"");
}