WorldToScreenPoint acting weird on Y.

I am drawing text boxes at the location of some game objects. When using WorldToScreenPoint the point that is being returned is fine on the X axis. However when the game object is at the bottom of the screen the text box float to the top of the screen.
Here is the code

void OnGUI()
{

	Vector3 screenTransform = m_cCamera.WorldToScreenPoint(transform.position);
	Rect txtRect = new Rect(screenTransform.x , screenTransform.y ,80,40);

// GUI.DrawTexture(txtRect,LargeTxtBubbleImg);

	if(new Rect(0,0,m_cCamera.camera.pixelWidth, m_cCamera.camera.pixelHeight).Contains(new Vector2(txtRect.x,txtRect.y)))
	{
		if(m_bTxtBubbleLarge == false)
		{
		
			if(GUI.Button(txtRect,screenTransform.y.ToString()))
			{
				m_bTxtBubbleLarge = true;
			}
		}
		else
		{
			
			txtRect.width *= 2;
			txtRect.height *= 2;	
			
			if(GUI.Button(txtRect, "NEW STORY!"))
			{
				m_bTxtBubbleLarge = false;	
			}
		}
	}
}

1 Answer

1

GUI coordinates and screen coordinates are not the same. GUI coordinates start in the upper left of the screen. Screen coordinates start in the lower left of the screen. Take a look at the GUIUtility class, and in particular the GUIUtility.ScreenToGUIPoint() function. GUI ‘Y’ will be Screen.height - Screen ‘Y’.