Skip to content

Commit 5cf5f0a

Browse files
fix
Fixing issue where Rigidbody2D was not applying rotation correctly. (4009) Fixing issue where `NetworkRigidbodyBase` was always checking the 3D rigid body's interpolation mode when determining if it is kinematic and needs to put the rigid body to sleep and then switch to interpolation. (3924)
1 parent 8c78fc3 commit 5cf5f0a

1 file changed

Lines changed: 46 additions & 18 deletions

File tree

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

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
22
using System.Runtime.CompilerServices;
33
using UnityEngine;
4+
using static Unity.Netcode.Components.NetworkTransform;
45

56
namespace Unity.Netcode.Components
67
{
@@ -571,7 +572,7 @@ private void InternalMoveRotation2D(Quaternion rotation)
571572
{
572573
var quaternion = Quaternion.identity;
573574
var angles = quaternion.eulerAngles;
574-
angles.z = m_InternalRigidbody2D.rotation;
575+
angles.z = rotation.z;
575576
quaternion.eulerAngles = angles;
576577
m_InternalRigidbody2D.MoveRotation(quaternion);
577578
}
@@ -845,6 +846,28 @@ public void SetIsKinematic(bool isKinematic)
845846
PostSetIsKinematic();
846847
}
847848

849+
850+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
851+
private bool HasInterpolationTypeSet(InterpolationTypes interpolationType)
852+
{
853+
#if COM_UNITY_MODULES_PHYSICS && COM_UNITY_MODULES_PHYSICS2D
854+
if (m_IsRigidbody2D)
855+
{
856+
return interpolationType == InterpolationTypes.Extrapolate ? m_InternalRigidbody2D.interpolation == RigidbodyInterpolation2D.Extrapolate : m_InternalRigidbody2D.interpolation == RigidbodyInterpolation2D.Interpolate;
857+
}
858+
else
859+
{
860+
return interpolationType == InterpolationTypes.Extrapolate ? m_InternalRigidbody.interpolation == RigidbodyInterpolation.Extrapolate : m_InternalRigidbody.interpolation == RigidbodyInterpolation.Interpolate;
861+
}
862+
#endif
863+
#if COM_UNITY_MODULES_PHYSICS && !COM_UNITY_MODULES_PHYSICS2D
864+
return interpolationType == InterpolationTypes.Extrapolate ? m_InternalRigidbody2D.interpolation == RigidbodyInterpolation2D.Extrapolate : m_InternalRigidbody2D.interpolation == RigidbodyInterpolation2D.Interpolate;
865+
#endif
866+
#if !COM_UNITY_MODULES_PHYSICS && COM_UNITY_MODULES_PHYSICS2D
867+
return interpolationType == InterpolationTypes.Extrapolate ? m_InternalRigidbody.interpolation == RigidbodyInterpolation.Extrapolate : m_InternalRigidbody.interpolation == RigidbodyInterpolation.Interpolate;
868+
#endif
869+
}
870+
848871
[MethodImpl(MethodImplOptions.AggressiveInlining)]
849872
private void PostSetIsKinematic()
850873
{
@@ -853,26 +876,31 @@ private void PostSetIsKinematic()
853876
{
854877
return;
855878
}
879+
856880
if (UseRigidBodyForMotion)
857881
{
858-
// Only if the NetworkTransform is set to interpolate do we need to check for extrapolation
859-
if (NetworkTransform.Interpolate && m_OriginalInterpolation == InterpolationTypes.Extrapolate)
882+
// Exit early if the original interpolation type is not set to extrapolate or NetworkTransform interpolate is disabled
883+
if (m_OriginalInterpolation != InterpolationTypes.Extrapolate || !NetworkTransform.Interpolate)
860884
{
861-
if (IsKinematic())
862-
{
863-
// If not already set to interpolate then set the Rigidbody to interpolate
864-
if (m_InternalRigidbody.interpolation == RigidbodyInterpolation.Extrapolate)
865-
{
866-
// Sleep until the next fixed update when switching from extrapolation to interpolation
867-
SleepRigidbody();
868-
SetInterpolation(InterpolationTypes.Interpolate);
869-
}
870-
}
871-
else
872-
{
873-
// Switch it back to the original interpolation if non-kinematic (doesn't require sleep).
874-
SetInterpolation(m_OriginalInterpolation);
875-
}
885+
return;
886+
}
887+
888+
// Otherwise, if this is the active physics object
889+
if (!IsKinematic())
890+
{
891+
// switch it back to the original interpolation and exit early
892+
SetInterpolation(m_OriginalInterpolation);
893+
}
894+
895+
// Determine if the rigid body being used is configured for extrapolation.
896+
var isRigidbodyExtrapolating = HasInterpolationTypeSet(InterpolationTypes.Extrapolate);
897+
898+
// If the Rigidbody or Rigidbody2D is currently configured to extrapolate
899+
if (HasInterpolationTypeSet(InterpolationTypes.Extrapolate))
900+
{
901+
// sleep until the next fixed update when switching from extrapolation to interpolation
902+
SleepRigidbody();
903+
SetInterpolation(InterpolationTypes.Interpolate);
876904
}
877905
}
878906
else

0 commit comments

Comments
 (0)