- Home /
zoom problem with GUI Box that follows a character
I have a GUI box following a sprite character in a 2d Top Down Game. If I zoom in, the GUI box goes up, if I zoom out, the GUI box moves down, instead of staying at the point it was drawn at game start. Have a look at the image below.
Some more details:
the camera is in orthographic mode (although the same happens in perspective mode)
the camera zoom is done by changing its OrthographicSize-value
This is the code I use to draw the GUI:
void OnGUI()
{
GUI.skin = guiSkin;
boxPosition = Camera.main.WorldToScreenPoint(baseAgent.transform.position);
guiSkin.box.fontSize = fontSize;
nameLabelText = baseAgent.name;
Vector2 content = guiSkin.box.CalcSize(new GUIContent(nameLabelText));
Vector3 boxPosition = new Vector3(Mathf.Clamp(boxPosition.x, 0 + (content.x / 2), Screen.width - (content.x / 2)),
Mathf.Clamp(boxPosition.y, 40, Screen.height), boxPosition.y);
GUILayout.BeginArea(new Rect((boxPosition.x + offsetX) - (content.x / 2), (Screen.height - boxPosition.y) + offsetY, content.x, content.y));
GUILayout.BeginHorizontal();
GUILayout.Box(nameLabelText);
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
Any clue on what might solve the issue would be greatly appreciated! Thanks!
gui-zoom-problem.png
(57.8 kB)
Comment
Best Answer
Answer by WillNode · Mar 24, 2015 at 10:06 PM
try to make BaseAgent
Sprite pivot in bottom of the sprite.