How to create player health
Understanding C# Properties
We all want our code to be super-accurate. For example, if the player’s health changes during gameplay. we want to know by how much and when. We don’t want to know by roughly how much, or 3 seconds after it all happened. That’s too little information and too late. We want an immediate and direct picture of a character’s health. So, it seems tempting to dump all health checking code inside an Update event. After all, Update happens every frame, right? We want to capture changes as soon as they happen- and so Update is perfect. But doing this is a big mistake. Really big.
Let’s fix this problem right away by learning C# Properties. They act like gateways to variables. We can create a variable (like HealthPoints) and have a gatekeeper watch for changes to them. When changes happen, we get notified. And we can even validate the changes to accept or reject them, protecting our variables from being assigned invalid values. Inappropriate values for health might be: -10 or 909979. We don’t want that and C# Properties are what’s needed to fix it. Check out our video above to learn about C# Properties. They’re really simple to learn.
Getting Started with Health
Okay. So you’ve learned about C# Properties. That’s great. Now we can apply them to creating a general purpose health script. It’s a script that you can attach to many things. This includes the player, NPCs and even inanimate objects- like swords, chairs and desks- to give them a durability quality. In making this script, we need to get away from using the Update function and instead rely on properties to detect changes to the health points.
Properties are great because they let us continue using variables as normal- in assignment and arithmetical statements, for example- but they now work like functions. We get to validate variable values, prevent invalid assignments, and even notify listeners when important events happen. Check out our YouTube video, as above, on creating a basic health script that uses C# Properties.
Move Further with Health
You’ve now got a solid Health Script foundation. You’ve seen how to make changes to health points easily using properties. This is much better than using the Update function to check values every frame. Perfect. Let’s make our Health Script better by supporting Events and Regeneration features.
Events are simply functions that run every time a key event happens. For Health, we’ll certainly want to run events for Health Change and Health Expired events. That is, whenever the health increases or decreases, and whenever the health reaches 0. On these events, we want to run potentially any kind of behaviour- perhaps different behaviours for different characters. The data type UnityEvent can help us do this!
In addition to events, we may want regeneration abilities. Specifically, for our health to generate continually, over time back to its full state. This is easy to do, and we can use Time.deltaTime to achieve it. Take a look at our YouTube tutorial on making Better Health Scripts, as above.
Excellent. You’ve now created a Health Script that supports property based health points, events and regeneration. This script can be attached to the player, NPCS and even to inanimate objects for supporting any health or durability based concepts.