Skip to content

Commit 67462b4

Browse files
committed
Health smoke sync & further simplified Ping using Mirror's rtt
1 parent 3082b0c commit 67462b4

2 files changed

Lines changed: 11 additions & 17 deletions

File tree

Assets/Game/Scripts/Network/GameNetworkPlayer.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff 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
{

Assets/Game/Scripts/UI/Ping.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
using System;
21
using UnityEngine;
32
using Mirror;
43
using UnityEngine.UI;
54

65
public 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
}

0 commit comments

Comments
 (0)