forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateHealthBarSystem.cs
More file actions
42 lines (38 loc) · 1.42 KB
/
Copy pathUpdateHealthBarSystem.cs
File metadata and controls
42 lines (38 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using Unity.Entities;
using Unity.Transforms;
using UnityEngine;
namespace Samples.HelloNetcode
{
#if !UNITY_DISABLE_MANAGED_COMPONENTS
/// <summary>
/// Update position and rotation of the health bar above players. This will make sure the health bar follow the character
/// character and is always facing the main camera.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
[UpdateInGroup(typeof(PresentationSystemGroup))]
[RequireMatchingQueriesForUpdate]
public partial struct UpdateHealthBarSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
if (Camera.main == null)
{
state.Enabled = false;
return;
}
var mainCamera = Camera.main;
foreach (var (ui, health, localTransform) in SystemAPI.Query<HealthUI, RefRO<Health>, RefRO<LocalTransform>>())
{
if (health.ValueRO.CurrentHitPoints <= 0)
{
continue;
}
ui.HealthBar.position = localTransform.ValueRO.Position + ui.Offset;
var n = mainCamera.transform.position - ui.HealthBar.position;
ui.HealthBar.rotation = Quaternion.LookRotation(n);
ui.HealthSlider.fillAmount = health.ValueRO.CurrentHitPoints / health.ValueRO.MaximumHitPoints;
}
}
}
#endif
}