The healthbar
|
This is my first entry for the blog assignment we got. I chose the healthbar for the first week, since I’ve recently implemented its functionality. What?The healthbar is a central mechanic in very many games. It’s purpose is to represent the players health, in other words how many hits he can take before the game is over. Healthbars have taken many different forms in many different games. The Call of Duty series for instance uses the screen itself to indicate how hurt you are. The screen will gradually turn red when you take damage, and when you stay safe for a while it goes back to normal, signifying that you are now at max health again. Some games just display the actual health value as a number. As you can see from the picture we chose to use a very tradition, and literal, health “bar”, more on why later. How?The actual slider, that is to say the UI element representing the healthbar, was actually in place very early on, but I sort of forgot about it as I moved onto other stuff. I only implemented it’s functionality this week, and I actually ran into some problems along the way. Adding the UI element was very easy, in fact it hardly required anything from me at all. Unity has a default graphic for sliders and that’s what I used. We will of course replace it at some point with our own artists work. I simply had to create a new UI element, choose “slider” and then just drag it into place. I also had to remove the so called “handle”. The handle is the component that you click and drag to manually change the value of a slider, but since the player never needs to do that (no cheating allowed!) it was best to simply remove it. I did however encounter some issues when trying to implement it’s functionality, mainly because I haven’t quite mastered classes yet. I didn’t actually understand exactly how initiating a variable of a class, that you yourself had created, worked. Once it was explained to me it became painfully obvious, but what I was doing was this: PlayerHealth(<-This is the class/script) playerHealth; (<-This is the variable) And I thought that was enough! Now for the programmers out there it’s obvious that this is, in fact, NOT enough. This line only reserves memory for the variable playerHealth of the type PlayerHealth, but that variable is never initiated so it’s defaulted to the value null. Which means that any function I call from that variable, while they will run, won’t do jack shit because there’s nothing there to change. I solved this issue by doing this: playerHealth = GetComponent(); Which assigns the variable its value. The way the slider works is that the sliders value is “linked” to the currentHealth variable. This variable is changed everytime the player gets damaged, and AFTER it’s been changed I then set the sliders value to currentHealths value, like so: healthSlider.value = currentHealth; Works like a charm! Why?In a game that involves fighting, like ours, it is absolutely essential to have some sort of health value in place, and some sort of indicator to show it to the player. Otherwise the player has no way of knowing how he’s doing, and without health there would be no failure state, which would make it impossible to lose. As I mentioned earlier we went for the traditional health bar. In the original concept document however the players health was displayed with three symbols, one representing one hit the player could take. We decided not to take that approach however. One; because three health points isn’t enough for our game in order fot it to be fair and two; because a health bar gives us more flexibility in regards to the damage that the different enemies inflict. So that was a brief summary of my work on our games healthbar. I’ll be back again next week for a review on another one of my tasks. So see you next week!
|