BGP Week 5: HUD!

A new week, and still a seemingly endless pile of tasks to be done! After discussing with my group about whether or not I should continue working on the level generation, we came to the conclusion that it was probably best to put me on other tasks instead, prioritizing having an actual game instead of something half-finished with one cool way of generating levels. Thus, the level generation from the earlier posts was put on the backburner indefinitely, and I took the task of creating the ingame UI.

Now, the HUD is probably what I’ve ended up doing on mostly every project, and therefore I feel quite accustomed to how they should work on a code level. This is made even easier thanks to the (somewhat) new UI tools in Unity, and even easier again since we want a somewhat conservative and standard racing UI, which means only having things like speed, time, lap counter and position. All of those can be displayed in text, which is what I ended up doing due to simplicity.

Of course, nothing code-related comes without problems. For this, I ran into problems with a) the UI clipping through objects in the world, and b) the UI canvas being larger than the camera frustum, which I guess are more problems with how Unity works versus how I want it to work, rather than my code.

The clipping issue was caused by two things:

  1. We wanted each player to have their own UI, meaning we couldn’t use the Screen Space – Overlay render mode
  2. I wanted to avoid having a separate camera only for the UI

After googling around for probably half a day to no avail, I ended up modifying the default shaders in Unity to disable the ZTest, causing them to always get drawn on top. I would have prefered having a built-in way of handling it, but meh, this works.

For the camera frustum issue, it was caused by the canvas inheriting the scale of the parent object (our “root” object for everything related to the player), which was set to 3,5. This makes sense after having it solved, since that’s how everything else works in Unity, but it totally confused me while trying to troubleshoot it. This was resolved simply by setting the scale to 1, and instead scaling the vehicle mesh.

As I’m reading this again, I can’t help but think that I really did nothing this week. Then I remember that I lost my progress like 3-4 times due to random Unity crashes, merge conflicts and the like, and suddenly I don’t feel so bad any more.