Skip to content

Commit 158821c

Browse files
fix
This resolves the issue where there was a de-synch between converting transform spaces.
1 parent 5a56eef commit 158821c

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed

com.unity.netcode.gameobjects/Runtime/Components/Interpolator/BufferedLinearInterpolator.cs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,24 +286,24 @@ private void ConvertInterpolateStateValues(Transform parent, bool inLocalSpace)
286286
}
287287

288288
[MethodImpl(MethodImplOptions.AggressiveInlining)]
289-
private void ConvertTransformSpace()
289+
private void ConvertTransformSpace(BufferedItem newTarget)
290290
{
291291
if (!AutoConvertTransformSpace)
292292
{
293293
return;
294294
}
295-
if (InterpolateState.TargetParent != Parent)
295+
if (InterpolateState.TargetParent != newTarget.MeasurementParent)
296296
{
297297
if (InterpolateState.TargetParent != null)
298298
{
299299
// Convert to world space or local space depending upon what our current parent is.
300300
ConvertInterpolateStateValues(InterpolateState.TargetParent, false);
301301
}
302302

303-
if (Parent != null)
303+
if (newTarget.MeasurementParent != null)
304304
{
305305
// Convert to local space.
306-
ConvertInterpolateStateValues(Parent, true);
306+
ConvertInterpolateStateValues(newTarget.MeasurementParent, true);
307307
}
308308
}
309309
}
@@ -366,7 +366,7 @@ private void TryConsumeFromBuffer(double renderTime, double minDeltaTime, double
366366
{
367367
if (m_BufferQueue.TryDequeue(out BufferedItem target))
368368
{
369-
ConvertTransformSpace();
369+
ConvertTransformSpace(target);
370370

371371
if (!InterpolateState.Target.HasValue)
372372
{
@@ -516,7 +516,7 @@ private void TryConsumeFromBuffer(double renderTime, double serverTime)
516516
{
517517
if (m_BufferQueue.TryDequeue(out BufferedItem target))
518518
{
519-
ConvertTransformSpace();
519+
ConvertTransformSpace(target);
520520
if (!InterpolateState.Target.HasValue)
521521
{
522522
InterpolateState.Target = target;
@@ -679,6 +679,29 @@ internal void AddMeasurement(Transform parent, T newMeasurement, double sentTime
679679
}
680680
}
681681

682+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
683+
private T GetParentRelativeValue(T currentValue)
684+
{
685+
if (!AutoConvertTransformSpace || InterpolateState.TargetParent == Parent)
686+
{
687+
return currentValue;
688+
}
689+
690+
// Just convert on the fly until the next state is reached where it will do a full
691+
// conversion when popped from the queue.
692+
if (InterpolateState.TargetParent)
693+
{
694+
currentValue = OnConvertTransformSpace(InterpolateState.TargetParent, currentValue, false);
695+
}
696+
697+
if (Parent != null)
698+
{
699+
var previousCurrent = currentValue.ToString();
700+
currentValue = OnConvertTransformSpace(Parent, currentValue, true);
701+
}
702+
return currentValue;
703+
}
704+
682705
/// <summary>
683706
/// Gets latest value from the interpolator. This is updated every update as time goes by.
684707
/// </summary>
@@ -689,16 +712,17 @@ public T GetInterpolatedValue()
689712
var currentValue = InterpolateState.CurrentValue;
690713
if (AutoConvertTransformSpace && InterpolateState.TargetParent != Parent)
691714
{
692-
// Just convert on the fly until the next state is reached where it will do a full
693-
// conversion when popped from the queue.
694-
if (InterpolateState.TargetParent != null)
695-
{
696-
currentValue = OnConvertTransformSpace(InterpolateState.TargetParent, currentValue, false);
697-
}
715+
currentValue = GetParentRelativeValue(currentValue);
698716

699-
if (Parent != null)
717+
// When there are no more states and we have reached our target,
718+
// hijack the last state as if it was submitted by the current
719+
// parent.
720+
if (m_BufferQueue.Count == 0 && (InterpolateState.TargetReached || !InterpolateState.Target.HasValue))
700721
{
701-
currentValue = OnConvertTransformSpace(Parent, currentValue, true);
722+
InterpolateState.CurrentValue = currentValue;
723+
InterpolateState.NextValue = currentValue;
724+
InterpolateState.PreviousValue = currentValue;
725+
InterpolateState.TargetParent = Parent;
702726
}
703727
}
704728
return currentValue;

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,12 +1526,12 @@ internal bool SynchronizeScale
15261526
/// <summary>
15271527
/// Returns true if position is currently in local space and false if it is in world space.
15281528
/// </summary>
1529-
protected bool PositionInLocalSpace => (!SwitchTransformSpaceWhenParented && InLocalSpace) || (m_PositionInterpolator != null && m_PositionInterpolator.InLocalSpace && SwitchTransformSpaceWhenParented);
1529+
protected bool PositionInLocalSpace => InLocalSpace;
15301530

15311531
/// <summary>
15321532
/// Returns true if rotation is currently in local space and false if it is in world space.
15331533
/// </summary>
1534-
protected bool RotationInLocalSpace => (!SwitchTransformSpaceWhenParented && InLocalSpace) || (m_RotationInterpolator != null && m_RotationInterpolator.InLocalSpace && SwitchTransformSpaceWhenParented);
1534+
protected bool RotationInLocalSpace => InLocalSpace;
15351535

15361536
/// <summary>
15371537
/// When enabled (default) interpolation is applied.
@@ -3146,10 +3146,12 @@ internal void ApplyUpdatedState(NetworkTransformState newState)
31463146
// depending upon whether UsePositionDeltaCompression is enabled
31473147
if (m_LocalAuthoritativeNetworkState.HasPositionChange)
31483148
{
3149+
// If interpolating, get the current value as the final next position or current position
3150+
// depending upon if the interpolator is still processing a state or not.
3151+
var newTargetPosition = Interpolate ? m_PositionInterpolator.GetInterpolatedValue() : m_TargetPosition;
31493152
if (!m_LocalAuthoritativeNetworkState.UseHalfFloatPrecision)
31503153
{
31513154
var position = m_LocalAuthoritativeNetworkState.GetPosition();
3152-
var newTargetPosition = m_TargetPosition;
31533155
if (m_LocalAuthoritativeNetworkState.HasPositionX)
31543156
{
31553157
newTargetPosition.x = position.x;
@@ -3164,8 +3166,8 @@ internal void ApplyUpdatedState(NetworkTransformState newState)
31643166
{
31653167
newTargetPosition.z = position.z;
31663168
}
3167-
m_TargetPosition = newTargetPosition;
31683169
}
3170+
m_TargetPosition = newTargetPosition;
31693171
UpdatePositionInterpolator(m_TargetPosition, sentTime);
31703172
}
31713173

@@ -3900,7 +3902,6 @@ internal override void InternalOnNetworkObjectParentChanged(NetworkObject parent
39003902
m_PositionInterpolator.AutoConvertTransformSpace = SwitchTransformSpaceWhenParented;
39013903
m_PositionInterpolator.InLocalSpace = InLocalSpace;
39023904
m_PositionInterpolator.Parent = InLocalSpace ? parentNetworkObject.transform : null;
3903-
var parentName = InLocalSpace ? parentNetworkObject.name : "root";
39043905

39053906
if (LastTickSync == m_LocalAuthoritativeNetworkState.GetNetworkTick())
39063907
{
@@ -3917,7 +3918,14 @@ internal override void InternalOnNetworkObjectParentChanged(NetworkObject parent
39173918
}
39183919
else
39193920
{
3920-
m_InternalCurrentPosition = m_TargetPosition = Interpolate ? m_PositionInterpolator.GetInterpolatedValue() : GetSpaceRelativePosition();
3921+
if (CanCommitToTransform)
3922+
{
3923+
m_InternalCurrentPosition = GetSpaceRelativePosition();
3924+
}
3925+
else
3926+
{
3927+
m_InternalCurrentPosition = m_TargetPosition = Interpolate ? m_PositionInterpolator.GetInterpolatedValue() : GetSpaceRelativePosition();
3928+
}
39213929
}
39223930
}
39233931

@@ -3942,7 +3950,14 @@ internal override void InternalOnNetworkObjectParentChanged(NetworkObject parent
39423950
}
39433951
else
39443952
{
3945-
m_InternalCurrentRotation = Interpolate ? m_RotationInterpolator.GetInterpolatedValue() : GetSpaceRelativeRotation();
3953+
if (CanCommitToTransform)
3954+
{
3955+
m_InternalCurrentRotation = GetSpaceRelativeRotation();
3956+
}
3957+
else
3958+
{
3959+
m_InternalCurrentRotation = Interpolate ? m_RotationInterpolator.GetInterpolatedValue() : GetSpaceRelativeRotation();
3960+
}
39463961
m_TargetRotation = m_InternalCurrentRotation.eulerAngles;
39473962
}
39483963
}
@@ -4207,7 +4222,7 @@ internal BufferedLinearInterpolatorQuaternion GetRotationInterpolator()
42074222
private void UpdateInterpolation()
42084223
{
42094224
// Select the time system relative to the type of NetworkManager instance.
4210-
var timeSystem = m_CachedNetworkManager.IsServer ? m_CachedNetworkManager.ServerTime : m_CachedNetworkManager.LocalTime;
4225+
var timeSystem = m_CachedNetworkManager.IsServer ? m_CachedNetworkManager.LocalTime : m_CachedNetworkManager.ServerTime;
42114226
var currentTime = timeSystem.Time;
42124227
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
42134228
var cachedDeltaTime = m_UseRigidbodyForMotion ? m_CachedNetworkManager.RealTimeProvider.FixedDeltaTime : m_CachedNetworkManager.RealTimeProvider.DeltaTime;

0 commit comments

Comments
 (0)