Skip to content

Commit 19d78c4

Browse files
fix: client network transform initialization (#1290)
* fix: client network transform initialization * restore private
1 parent b2e08c1 commit 19d78c4

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

com.unity.netcode.gameobjects/Components/NetworkTransform.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -683,17 +683,8 @@ private void Awake()
683683
{
684684
m_Transform = transform;
685685

686-
// ReplNetworkState.NetworkVariableChannel = NetworkChannel.PositionUpdate; // todo figure this out, talk with Matt/Fatih, this should be unreliable
687-
688-
m_ReplicatedNetworkState.OnValueChanged += OnNetworkStateChanged;
689-
}
690-
691-
public override void OnNetworkSpawn()
692-
{
693-
CanCommitToTransform = IsServer;
694-
m_CachedIsServer = IsServer;
695-
m_CachedNetworkManager = NetworkManager;
696-
686+
// we only want to create our interpolators during Awake so that, when pooled, we do not create tons
687+
// of gc thrash each time objects wink out and are re-used
697688
m_PositionXInterpolator = new BufferedLinearInterpolatorFloat();
698689
m_PositionYInterpolator = new BufferedLinearInterpolatorFloat();
699690
m_PositionZInterpolator = new BufferedLinearInterpolatorFloat();
@@ -711,14 +702,32 @@ public override void OnNetworkSpawn()
711702
m_AllFloatInterpolators.Add(m_ScaleYInterpolator);
712703
m_AllFloatInterpolators.Add(m_ScaleZInterpolator);
713704
}
705+
}
706+
707+
public override void OnNetworkSpawn()
708+
{
709+
m_ReplicatedNetworkState.OnValueChanged += OnNetworkStateChanged;
710+
711+
CanCommitToTransform = IsServer;
712+
m_CachedIsServer = IsServer;
713+
m_CachedNetworkManager = NetworkManager;
714+
714715
if (CanCommitToTransform)
715716
{
716717
TryCommitTransformToServer(m_Transform, m_CachedNetworkManager.LocalTime.Time);
717718
}
718719
m_LocalAuthoritativeNetworkState = m_ReplicatedNetworkState.Value;
720+
721+
// crucial we do this to reset the interpolators so that recycled objects when using a pool will
722+
// not have leftover interpolator state from the previous object
719723
Initialize();
720724
}
721725

726+
public override void OnNetworkDespawn()
727+
{
728+
m_ReplicatedNetworkState.OnValueChanged -= OnNetworkStateChanged;
729+
}
730+
722731
public override void OnGainedOwnership()
723732
{
724733
Initialize();
@@ -743,13 +752,6 @@ private void Initialize()
743752
}
744753
}
745754

746-
public override void OnDestroy()
747-
{
748-
m_ReplicatedNetworkState.OnValueChanged -= OnNetworkStateChanged;
749-
750-
base.OnDestroy();
751-
}
752-
753755
#region state set
754756

755757
/// <summary>

com.unity.netcode.gameobjects/Runtime/Timing/NetworkTime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public NetworkTime ToFixedTime()
105105
return new NetworkTime(m_TickRate, m_CachedTick);
106106
}
107107

108-
public NetworkTime TimeTicksAgo(uint ticks)
108+
public NetworkTime TimeTicksAgo(int ticks)
109109
{
110110
return this - new NetworkTime(TickRate, ticks);
111111
}

com.unity.netcode.gameobjects/Samples/ClientNetworkTransform/Scripts/ClientNetworkTransform.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public class ClientNetworkTransform : NetworkTransform
1818
/// </summary>
1919
// This is public to make sure that users don't depend on this IsClient && IsOwner check in their code. If this logic changes in the future, we can make it invisible here
2020

21-
private void Awake()
21+
public override void OnNetworkSpawn()
2222
{
23+
base.OnNetworkSpawn();
2324
CanCommitToTransform = IsOwner;
2425
}
2526

0 commit comments

Comments
 (0)