Guide
Vitals and Stamina
Give the player health that can be damaged, healed and revived, and a sprint stamina based on heart rate. Sprinting raises the pulse, and when it peaks the player is exhausted until the pulse recovers. In this guide you add PlayerVitals, check its settings and connect a health bar.
On this page
Add the component#
- Select the player root, the GameObject with
PlayerLocomotion. - Add
PlayerVitals. The Setup Wizard does not add it. - Press Play. Health starts at Max Health.
Health#
maxHealthfloat default 100, minimum 1- Maximum health, which is also the starting value.
| Member | Returns | Behaviour |
|---|---|---|
ApplyDamage(float amount) | bool | Reduces health. Returns false and does nothing when the player is dead, the amount is zero or negative, or the amount is NaN or infinite. |
Heal(float amount) | bool | Restores health with the same rules as ApplyDamage. |
Revive() | bool | Restores full health to a dead player. Returns false when the player is alive. |
SetHealth(float value) | void | Sets the health, clamped between 0 and MaxHealth. Use it to mirror a value from a server. |
Health, MaxHealth, NormalizedHealth | float | Current, maximum and health as a 0 to 1 fraction. |
IsDead | bool | true at zero health. |
While the player is dead, PlayerLocomotion ignores movement and jump input.
Heart-rate stamina#
The stamina is modelled by PlayerExertion, a field of PlayerVitals. Sprinting only raises the pulse while the player is moving. The pulse is shown in beats per minute (Bpm).
- Sprint. The pulse rises by Rise Per Second until it reaches Maximum Bpm.
- Exhaustion. At Maximum Bpm the player is exhausted and
CanSprintbecomesfalse. The pulse stays at its peak for Peak Hold Seconds. - Release. The hold ends and the pulse must drop to Resume Bpm. The sprint key must also be released once before sprint works again.
- Recovery. After you stop sprinting, the pulse waits Recovery Delay Seconds. It then drops toward Walking Bpm while you walk, or toward Resting Bpm while you stand.
restingBpmdefault 72- Pulse while standing still. It is also the starting pulse.
walkingBpmdefault 90- Pulse the player settles at while walking.
maximumBpmdefault 176- Pulse at which the player is exhausted and sprint locks.
resumeBpmdefault 118- Pulse below which an exhausted player may sprint again, after the release.
risePerSeconddefault 8- Pulse increase per second while sprinting.
walkingRecoveryPerSeconddefault 5,restingRecoveryPerSeconddefault 7- Pulse decrease per second while walking and while standing still.
peakHoldSecondsdefault 3- Seconds the pulse holds at its peak after exhaustion.
recoveryDelaySecondsdefault 1.5- Seconds after a sprint before the pulse starts to recover.
The Inspector keeps these values in a consistent order. For example, Maximum Bpm is always at least 40 above Resting Bpm, and Resume Bpm stays below Maximum Bpm.
Events#
| C# event | Payload | UnityEvent | Fires |
|---|---|---|---|
HealthChanged | float (new health) | OnHealthChanged | When health changes, from damage, healing, revive or SetHealth |
Died | none | OnDied | When health reaches zero |
Revived | none | OnRevived | When a dead player gets health again |
Connect a health bar#
Subscribe to HealthChanged from code, or wire OnHealthChanged to a method in the Inspector. The bar reads NormalizedHealth, which is the value between 0 and 1:
using Player;
using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
[SerializeField] private PlayerVitals vitals;
[SerializeField] private Slider slider;
private void OnEnable()
{
vitals.HealthChanged += Refresh;
Refresh(vitals.Health);
}
private void OnDisable()
{
vitals.HealthChanged -= Refresh;
}
private void Refresh(float _)
{
slider.value = vitals.NormalizedHealth;
}
}
Read vitals.Bpm and vitals.CanSprint in Update to show the pulse or a sprint-locked icon.
Multiplayer#
Health is local. The FishNet and PurrNet add-ons do not synchronize it. On a networked game, let the server apply damage and mirror the value to clients with SetHealth.