Skip to content

Commit b776c54

Browse files
fix: NetworkRigidbodyBase 2D rigid body issues (#4012)
* 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) * style Removing static using directive (not used). * update Missed the return... * test Did a bit of an overhaul on the original NetworkRigidbodyTest. It should now run with distributed authority tests and includes a 2D rigidbody in the tests as well. * stye Removing trailing space on comment * test Updating the NetworkRigidbodyTest to do a 2nd spawn pass that swaps the session owner and non-session owner clients to validate it works when a non-session authority client is the spawn authority. * update Removing Rigidbody tests no longer used and already covered by `NetworkRigidbodyTests`. Moving `RigidbodyContactEventManagerTests` into its own file.
1 parent 7215237 commit b776c54

15 files changed

Lines changed: 613 additions & 940 deletions

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

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Runtime.CompilerServices;
33
using UnityEngine;
44

5+
56
namespace Unity.Netcode.Components
67
{
78
/// <summary>
@@ -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,29 @@ 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+
return;
894+
}
895+
896+
// If the Rigidbody or Rigidbody2D is currently configured to extrapolate
897+
if (HasInterpolationTypeSet(InterpolationTypes.Extrapolate))
898+
{
899+
// sleep until the next fixed update when switching from extrapolation to interpolation
900+
SleepRigidbody();
901+
SetInterpolation(InterpolationTypes.Interpolate);
876902
}
877903
}
878904
else

com.unity.netcode.gameobjects/Tests/Runtime/Physics/NetworkRigidbody2DTest.cs

Lines changed: 0 additions & 80 deletions
This file was deleted.

com.unity.netcode.gameobjects/Tests/Runtime/Physics/NetworkRigidbody2DTest.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)