Skip to content

Commit 9acf82d

Browse files
update
working version that needs clean up
1 parent d347eb2 commit 9acf82d

File tree

1 file changed

+73
-32
lines changed

1 file changed

+73
-32
lines changed

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

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ internal bool IsTeleportingNextFrame
208208
internal double SentTime;
209209

210210
internal bool IsDirty;
211+
internal int ExtrapolateTick;
211212

212213
/// <summary>
213214
/// This will reset the NetworkTransform BitSet
@@ -423,6 +424,9 @@ internal NetworkVariable<NetworkTransformState> ReplicatedNetworkState
423424
private int m_LastSentTick;
424425
private NetworkTransformState m_LastSentState;
425426

427+
// Used by the non-authoritative side to handle ending extrapolation (replaces server sending)
428+
private NetworkTransformState m_LastReceivedState;
429+
426430
internal NetworkTransformState GetLastSentState()
427431
{
428432
return m_LastSentState;
@@ -483,32 +487,11 @@ private void TryCommitTransform(Transform transformToCommit, double dirtyTime)
483487
NetworkLog.LogError($"[{name}] is trying to commit the transform without authority!");
484488
return;
485489
}
486-
var isDirty = ApplyTransformToNetworkState(ref m_LocalAuthoritativeNetworkState, dirtyTime, transformToCommit);
487490

488-
// if dirty, send
489-
// if not dirty anymore, but hasn't sent last value for limiting extrapolation, still set isDirty
490-
// if not dirty and has already sent last value, don't do anything
491-
// extrapolation works by using last two values. if it doesn't receive anything anymore, it'll continue to extrapolate.
492-
// This is great in case there's message loss, not so great if we just don't have new values to send.
493-
// the following will send one last "copied" value so unclamped interpolation tries to extrapolate between two identical values, effectively
494-
// making it immobile.
495-
if (isDirty)
491+
if (ApplyTransformToNetworkState(ref m_LocalAuthoritativeNetworkState, dirtyTime, transformToCommit))
496492
{
497493
// Commit the state
498494
ReplicatedNetworkState.Value = m_LocalAuthoritativeNetworkState;
499-
m_HasSentLastValue = false;
500-
m_LastSentTick = m_CachedNetworkManager.LocalTime.Tick;
501-
m_LastSentState = m_LocalAuthoritativeNetworkState;
502-
}
503-
else if (!m_HasSentLastValue && m_CachedNetworkManager.LocalTime.Tick >= m_LastSentTick + 1) // check for state.IsDirty since update can happen more than once per tick. No need for client, RPCs will just queue up
504-
{
505-
// Since the last m_LocalAuthoritativeNetworkState could have included a IsTeleportingNextFrame
506-
// we need to reset this here so only the deltas are applied and interpolation is not reset again.
507-
m_LastSentState.IsTeleportingNextFrame = false;
508-
m_LastSentState.SentTime = m_CachedNetworkManager.LocalTime.Time; // time 1+ tick later
509-
// Commit the state
510-
ReplicatedNetworkState.Value = m_LastSentState;
511-
m_HasSentLastValue = true;
512495
}
513496
}
514497

@@ -671,26 +654,33 @@ private void ApplyAuthoritativeState()
671654
{
672655
interpolatedPosition.x = isTeleporting || !Interpolate ? networkState.PositionX : m_PositionXInterpolator.GetInterpolatedValue();
673656
}
657+
else if (Interpolate && SyncPositionX)
658+
{
659+
interpolatedPosition.x = m_PositionXInterpolator.GetInterpolatedValue();
660+
}
674661

675662
if (networkState.HasPositionY)
676663
{
677664
interpolatedPosition.y = isTeleporting || !Interpolate ? networkState.PositionY : m_PositionYInterpolator.GetInterpolatedValue();
678665
}
666+
else if (Interpolate && SyncPositionY)
667+
{
668+
interpolatedPosition.y = m_PositionYInterpolator.GetInterpolatedValue();
669+
}
679670

680671
if (networkState.HasPositionZ)
681672
{
682673
interpolatedPosition.z = isTeleporting || !Interpolate ? networkState.PositionZ : m_PositionZInterpolator.GetInterpolatedValue();
683674
}
675+
else if (Interpolate && SyncPositionZ)
676+
{
677+
interpolatedPosition.z = m_PositionZInterpolator.GetInterpolatedValue();
678+
}
684679

685680
// Update the rotation values that were changed in this state update
686681
if (networkState.HasRotAngleChange)
687682
{
688-
var eulerAngles = new Vector3();
689-
if (Interpolate)
690-
{
691-
eulerAngles = m_RotationInterpolator.GetInterpolatedValue().eulerAngles;
692-
}
693-
683+
var eulerAngles = Interpolate ? m_RotationInterpolator.GetInterpolatedValue().eulerAngles : new Vector3();
694684
if (networkState.HasRotAngleX)
695685
{
696686
interpolatedRotAngles.x = isTeleporting || !Interpolate ? networkState.RotAngleX : eulerAngles.x;
@@ -706,6 +696,25 @@ private void ApplyAuthoritativeState()
706696
interpolatedRotAngles.z = isTeleporting || !Interpolate ? networkState.RotAngleZ : eulerAngles.z;
707697
}
708698
}
699+
else if (Interpolate && SyncRotAngleX || SyncRotAngleY || SyncRotAngleZ)
700+
{
701+
var eulerAngles = m_RotationInterpolator.GetInterpolatedValue().eulerAngles;
702+
interpolatedRotAngles = eulerAngles;
703+
if (SyncRotAngleY)
704+
{
705+
interpolatedRotAngles.x = m_RotationInterpolator.GetInterpolatedValue().eulerAngles.x;
706+
}
707+
708+
if (SyncRotAngleY)
709+
{
710+
interpolatedRotAngles.y = m_RotationInterpolator.GetInterpolatedValue().eulerAngles.y;
711+
}
712+
713+
if (SyncRotAngleY)
714+
{
715+
interpolatedRotAngles.y = m_RotationInterpolator.GetInterpolatedValue().eulerAngles.y;
716+
}
717+
}
709718

710719
// Update all scale axis that were changed in this state update
711720
if (networkState.HasScaleX)
@@ -724,11 +733,10 @@ private void ApplyAuthoritativeState()
724733
}
725734

726735
// Apply the new position
727-
if (networkState.HasPositionChange)
736+
if (networkState.HasPositionChange || Interpolate && (SyncPositionX || SyncPositionY || SyncPositionZ))
728737
{
729738
if (InLocalSpace)
730739
{
731-
732740
transform.localPosition = interpolatedPosition;
733741
}
734742
else
@@ -738,7 +746,7 @@ private void ApplyAuthoritativeState()
738746
}
739747

740748
// Apply the new rotation
741-
if (networkState.HasRotAngleChange)
749+
if (networkState.HasRotAngleChange || Interpolate && (SyncRotAngleX || SyncRotAngleY || SyncRotAngleZ))
742750
{
743751
if (InLocalSpace)
744752
{
@@ -751,12 +759,18 @@ private void ApplyAuthoritativeState()
751759
}
752760

753761
// Apply the new scale
754-
if (networkState.HasScaleChange)
762+
if (networkState.HasScaleChange || Interpolate && (SyncScaleX || SyncScaleY || SyncScaleZ))
755763
{
756764
transform.localScale = interpolatedScale;
757765
}
758766
}
759767

768+
[ServerRpc]
769+
private void UpdateServerWithAppliedRotationServerRpc(Vector3 eulerAngles)
770+
{
771+
Debug.Log($"[{name}] client updated their rotation to ({eulerAngles})");
772+
}
773+
760774
/// <summary>
761775
/// Only non-authoritative instances should invoke this
762776
/// </summary>
@@ -910,10 +924,31 @@ private void AddInterpolatedState(NetworkTransformState newState)
910924
}
911925

912926
currentRotation.eulerAngles = currentEulerAngles;
927+
if (!IsServer && IsOwner)
928+
{
929+
UpdateServerWithUpdatedRotationServerRpc(currentEulerAngles);
930+
}
913931
m_RotationInterpolator.AddMeasurement(currentRotation, sentTime);
914932
}
915933
}
916934

935+
[ServerRpc]
936+
private void UpdateServerWithUpdatedRotationServerRpc(Vector3 eulerAngles)
937+
{
938+
Debug.Log($"[{name}] Client notified it updated rotation interpolator with ({eulerAngles})");
939+
}
940+
941+
private void ApplyLastState()
942+
{
943+
if (!m_LastReceivedState.IsDirty || m_LastReceivedState.ExtrapolateTick >= NetworkManager.LocalTime.Tick)
944+
{
945+
return;
946+
}
947+
m_LastReceivedState.SentTime += 1.0 / NetworkManager.NetworkConfig.TickRate;
948+
AddInterpolatedState(m_LastReceivedState);
949+
m_LastReceivedState.ClearBitSetForNextTick();
950+
}
951+
917952
/// <summary>
918953
/// Only non-authoritative instances should invoke this method
919954
/// </summary>
@@ -932,7 +967,10 @@ private void OnNetworkStateChanged(NetworkTransformState oldState, NetworkTransf
932967

933968
if (Interpolate)
934969
{
970+
ApplyLastState();
935971
AddInterpolatedState(newState);
972+
m_LastReceivedState = newState;
973+
m_LastReceivedState.ExtrapolateTick = NetworkManager.LocalTime.Tick;
936974
}
937975
}
938976

@@ -1207,6 +1245,9 @@ protected virtual void Update()
12071245
}
12081246
// Now apply the current authoritative state
12091247
ApplyAuthoritativeState();
1248+
1249+
1250+
ApplyLastState();
12101251
}
12111252
}
12121253

0 commit comments

Comments
 (0)