The objective here was to create a target dummy for me to shoot with my laser gun. The target needed a GUI health component and a score when it dies. I wanted to mock this up fairly quickly and this is what I came up with:
The Target Capsule
The Target Capsule has three core components on it that make it work: Health, Take Damage, and Death Handler. At this stage, all of these components are super simple as you’ll see in a moment.
Attached as a child of the Target Capsule is a Text Mesh Pro game object which I use as the Health HUD for the target. That particular component references the Health component of the Target Capsule object.
The script here is fairly simple, when directed the Current Health of the capsule is retrieved and the text of the GUI object is updated.
The Health Component
It’s the job of the health component to track the current and max health of the Target Capsule and invoke the Health Changed and Zero Health events to notify whatever game object cares to listen when those events happen (the Health HUD, Score HUD, and Death Handler at this time).
The Take Damage Component
So this component could be more accurately named “CollisionHandler” and that refactoring is likely to take place tomorrow. If that happens, I will use a UnityEvent that sends the colliding object as a parameter to the listeners. More on this when it happens tomorrow.
Not a lot to celebrate here. Future iterations of this method will likely send the offed game object into a pool for recycling.
Like the Ammo HUD, this object will eventually need to be dynamically added as a listener to objects that will score points. For now it is referenced as part of the Zero Health event of the Target Capsule.
Almost not worth mentioning at this point by the Laser Rifle now has a Damage component which in this iteration simply encapsulates a public property. In the future I can see this doing something awesome like adding an On Fire component to a target that deals DOT with a cool burning effect.