Health System - BNS-MarkUlrich/MarksMagicToolbox GitHub Wiki
HealthData.cs is a Unity C# class designed to manage health-related data for game characters or entities. It includes functionality for tracking health, handling damage, and managing related events.
To use HealthData.cs in your Unity project, follow these steps:
- Import the
HealthData.csfile into your Unity project's scripts directory with this package.
| Property | Description |
|---|---|
Health |
The current health of the entity. |
MaxHealth |
The maximum health of the entity. |
HasMaxHealth |
Indicates if the entity's health has reached its maximum value. |
| Event | Description |
|---|---|
OnHealthChanged |
Event triggered when the entity's health changes. |
OnHealthAdded |
Event triggered when health is added to the entity. |
OnDamageTaken |
Event triggered when damage is inflicted on the entity. |
OnDie |
Event triggered when the entity dies. |
OnResurrected |
Event triggered when the entity is resurrected. |
| Method | Description |
|---|---|
AddHealth(float healthAmount) |
Adds health to the entity. Handles over-healing if allowed. |
TakeDamage(float damage) |
Inflicts damage on the entity. |
Resurrect(float newHealth) |
Resurrects the entity with specified health. |
Kill() |
Sets the entity's health to zero, marking it as dead. |
DestroySelf() |
Destroys the game object associated with the entity. |
// Example implementation
public class Character : MonoBehaviour
{
private HealthData healthData;
private void Start()
{
healthData = GetComponent<HealthData>();
// Subscribe to events or manipulate health as needed
healthData.onHealthChanged.AddListener(OnHealthChanged);
}
private void OnHealthChanged(HealthEvent healthEvent)
{
// Handle health change event
Debug.Log("Health changed to: " + healthEvent.currenthealth);
}
}The HealthData.cs class extends the MonoBehaviour class in Unity and includes various methods and events to handle health-related functionalities. It utilizes serialized fields to manage health and UnityEvents to trigger specific actions based on health changes and entity status.
Upon instantiation, the InitHealth method sets the maximum health to the initial health value provided.
The class handles health modifications via AddHealth, TakeDamage, Resurrect, Die, Kill, and DestroySelf methods, triggering relevant events (onHealthChanged, onHealthAdded, onDamageTaken, onDie, onResurrected) accordingly.
By leveraging this class, game developers can seamlessly manage health-related operations for their in-game entities within the Unity environment.
Learn about a class designed to manage health-related data for game characters or entities.