File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ public class GameNetworkPlayer : NetworkBehaviour
2424
2525 [ Header ( "Synced vars" ) ] [ SyncVar ] public string playerName ;
2626 [ SyncVar ] public int score ;
27- [ SyncVar ] public float health ;
27+ [ SyncVar ( hook = nameof ( OnSyncHealth ) ) ] public float health ;
2828
2929 private NetworkIdentity _identity ;
3030 private ArenaUi _arenaUi ;
@@ -229,6 +229,15 @@ public void SetHealth(float setHealth)
229229 RpcDisplayHealth ( setHealth ) ;
230230 }
231231
232+ private void OnSyncHealth ( float oldHealth , float newHealth )
233+ {
234+ // Hook gets invoked before Start configures _vehiclePhysics
235+ if ( ! ReferenceEquals ( _vehiclePhysics , null ) )
236+ {
237+ _vehiclePhysics . health = newHealth ;
238+ }
239+ }
240+
232241 [ ClientRpc ]
233242 public void RpcDisplayScore ( int displayScore )
234243 {
Original file line number Diff line number Diff line change 1- using System ;
21using UnityEngine ;
32using Mirror ;
43using UnityEngine . UI ;
54
65public class Ping : NetworkBehaviour
76{
87 public Text pingText ;
9- [ Range ( 0 , 10 ) ] public float intervalByServer = 0.5f ;
10- public float ping ;
11- private float _lastSync ;
128
139 private void Start ( )
1410 {
@@ -17,17 +13,6 @@ private void Start()
1713
1814 void Update ( )
1915 {
20- if ( isServer && Time . time - _lastSync > intervalByServer )
21- {
22- _lastSync = Time . time ;
23- RpcReceivePacket ( DateTimeOffset . UtcNow . ToUnixTimeMilliseconds ( ) ) ;
24- }
25- }
26-
27- [ ClientRpc ]
28- public void RpcReceivePacket ( long unixTimeMillis )
29- {
30- ping = DateTimeOffset . UtcNow . ToUnixTimeMilliseconds ( ) - unixTimeMillis ;
31- pingText . text = $ "Ping : { ping } ms";
16+ pingText . text = $ "Ping : { ( int ) ( NetworkTime . rtt * 1000 / 2 ) } ms";
3217 }
3318}
You can’t perform that action at this time.
0 commit comments