Monday, October 31, 2011

HUD in Game

We want to Create Really Simple but cool HUD in game to show the current score and speed level of the player. Because our game is pretty like the racing game, the idea of build a speedometer like HUD comes to our mind first.

Rotate the Pointer:
We started to add our HUD from a Unity GUI Texture, but the problem is, it is not allowed to rotate any non-3D GUI Textures in Unity, while we definitely don't want to use mesh textures because that is not cool...
So one way to rotate the GUI Texture is like that:

        posPointer = new Vector2(transform.localPosition.x - sizePointer.x * 0.5f, transform.localPosition.y);
        rectPointer = new Rect(posPointer.x - sizePointer.x * 0.5f, posPointer.y - sizePointer.y * 0.5f, sizePointer.x, sizePointer.y);
        pivotPointer = new Vector2(rectPointer.xMin + rectPointer.width * 0.5f, rectPointer.yMin + rectPointer.height * 0.5f);
        GUIUtility.RotateAroundPivot(angle, pivotPointer);
        GUI.DrawTexture(rectPointer, pointer);
        GUI.matrix = matrixBackup;
        GUI.DrawTexture(rectSpeedometer, speedometer);

It Rotates a Texture2D like the OpenGL way, and the result is pleasing.

Here is a screenshot of our HUD:

No comments:

Post a Comment