From World to Screen

Hi all.

I am trying to code something like “enemy hit” effect. To start, I try to make a text that floats under the enemy. However, the position of the text is far from the object and moves in different direction as the enemy moves. Any explanation for this? The project is very simple: a camera, a plane and the moving enemy.

Code in the enemy’s script:

	void OnGUI(){
		Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
		GUI.Box(new Rect(pos.x, pos.y, 30, 30), "Hit");
	}

Case Solved after some more research:
change pos.y to Screen.height - pos.y due to GUI coordinate is different from screen coordinate.

what about the pos.x then? shouldent that be change also??