Im having issues with “WorldToScreenPoint”, The X and Z are fine but Y is messed up. Seen here (the red box, meant to be on the tank at all times):
http://tristanjc.com/unity/newweb.html
Just using:
void Update () {
if(target){
guiPos = cam.WorldToScreenPoint(target.position);
}
}
void OnGUI(){
GUI.DrawTexture(new Rect(guiPos.x - 12.5f,guiPos.y - 12.5f,50f,50f),targetTexture);
}
You will need to use some inverse stuff to translate the World To Screen Point to the GUI correctly.
Example:
Vector2 screenPosition;
int screenHeightInt;
void Start()
{
screenHeightInt = Screen.height;
}
void OnGUI()
{
//now you can use the position as expected
//apply your code here
GUI.BeginGroup (new Rect(screenPosition.x - 25, screenPosition.y, size.x, size.y), emptyTex, healthBarStyle);
}
void Update()
{
//Get Position of the Object
screenPosition = mainCam.WorldToScreenPoint(healthBarObject.transform.position);
//Get inverse of y-position
screenPosition.y = screenHeightInt - (screenPosition.y + 1);
}