- Home /
Having issues with "WorldToScreenPoint"
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);
 }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by RyanZimmerman87 · Oct 17, 2013 at 10:34 PM
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);
     }
     
     
Your answer
 
 
             Follow this Question
Related Questions
Method Must have a Return Type (Vector3 x,y,z) 2 Answers
Why is my GUI pushed over to the right? 0 Answers
Issue's With Positioning 0 Answers
Distribute terrain in zones 3 Answers
Guided missile help c# 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                