File tree Expand file tree Collapse file tree 4 files changed +33
-22
lines changed Expand file tree Collapse file tree 4 files changed +33
-22
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
+ using System . Collections ;
2
3
using Unity . Netcode ;
3
4
using Unity . Netcode . Components ;
4
5
using UnityEngine ;
@@ -22,7 +23,18 @@ public override void OnNetworkSpawn()
22
23
{
23
24
if ( IsServer )
24
25
{
25
- m_NetworkLifeState . LifeState . OnValueChanged += OnLifeStateChanged ;
26
+ // Wait until next frame before registering on OnValueChanged to make sure NetworkAnimator has spawned before.
27
+ StartCoroutine ( WaitToRegisterOnLifeStateChanged ( ) ) ;
28
+ }
29
+ }
30
+
31
+ IEnumerator WaitToRegisterOnLifeStateChanged ( )
32
+ {
33
+ yield return new WaitForEndOfFrame ( ) ;
34
+ m_NetworkLifeState . LifeState . OnValueChanged += OnLifeStateChanged ;
35
+ if ( m_NetworkLifeState . LifeState . Value != LifeState . Alive )
36
+ {
37
+ OnLifeStateChanged ( LifeState . Alive , m_NetworkLifeState . LifeState . Value ) ;
26
38
}
27
39
}
28
40
Original file line number Diff line number Diff line change @@ -85,6 +85,7 @@ public override void OnNetworkSpawn()
85
85
var startingAction = new ActionRequestData ( ) { ActionTypeEnum = m_StartingAction } ;
86
86
PlayAction ( ref startingAction ) ;
87
87
}
88
+ InitializeHitPoints ( ) ;
88
89
}
89
90
}
90
91
@@ -105,6 +106,24 @@ public override void OnNetworkDespawn()
105
106
}
106
107
}
107
108
109
+ void InitializeHitPoints ( )
110
+ {
111
+ NetState . HitPoints = NetState . CharacterClass . BaseHP . Value ;
112
+
113
+ if ( ! IsNpc )
114
+ {
115
+ SessionPlayerData ? sessionPlayerData = SessionManager < SessionPlayerData > . Instance . GetPlayerData ( OwnerClientId ) ;
116
+ if ( sessionPlayerData is { HasCharacterSpawned : true } )
117
+ {
118
+ NetState . HitPoints = sessionPlayerData . Value . CurrentHitPoints ;
119
+ if ( NetState . HitPoints <= 0 )
120
+ {
121
+ NetState . LifeState = LifeState . Fainted ;
122
+ }
123
+ }
124
+ }
125
+ }
126
+
108
127
/// <summary>
109
128
/// Play a sequence of actions!
110
129
/// </summary>
Original file line number Diff line number Diff line change @@ -141,7 +141,7 @@ public bool IsMoving()
141
141
/// </summary>
142
142
public void CancelMove ( )
143
143
{
144
- m_NavPath . Clear ( ) ;
144
+ m_NavPath ? . Clear ( ) ;
145
145
m_MovementState = MovementState . Idle ;
146
146
}
147
147
Original file line number Diff line number Diff line change @@ -110,26 +110,6 @@ public LifeState LifeState
110
110
/// </summary>
111
111
public event Action < Vector3 > ReceivedClientInput ;
112
112
113
- public override void OnNetworkSpawn ( )
114
- {
115
- if ( ! IsServer ) return ;
116
- HitPoints = GetInitialHitPoints ( ) ;
117
- }
118
-
119
- int GetInitialHitPoints ( )
120
- {
121
- if ( ! IsNpc )
122
- {
123
- SessionPlayerData ? sessionPlayerData = SessionManager < SessionPlayerData > . Instance . GetPlayerData ( OwnerClientId ) ;
124
- if ( sessionPlayerData is { HasCharacterSpawned : true } )
125
- {
126
- return sessionPlayerData . Value . CurrentHitPoints ;
127
- }
128
- }
129
-
130
- return CharacterClass . BaseHP . Value ;
131
- }
132
-
133
113
/// <summary>
134
114
/// RPC to send inputs for this character from a client to a server.
135
115
/// </summary>
You can’t perform that action at this time.
0 commit comments