Skip to content

Commit

Permalink
chore: update rigidbody deprecations (#3199)
Browse files Browse the repository at this point in the history
* update

Changing RigidBody2D kinematic checks to account for deprecation of isKinematic.

* update

Adding changelog
  • Loading branch information
NoelStephensUnity authored Jan 10, 2025
1 parent cefecf2 commit bd738b3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

- Fixed issue where `NetworkRigidBody2D` was still using the deprecated `isKinematic` property in Unity versions 2022.3 and newer. (#3199)
- Fixed issue where an exception was thrown when calling `NetworkManager.Shutdown` after calling `UnityTransport.Shutdown`. (#3118)

### Changed
Expand Down
13 changes: 11 additions & 2 deletions com.unity.netcode.gameobjects/Components/NetworkRigidbody2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ private void SetupRigidBody()
// Turn off physics for the rigid body until spawned, otherwise
// clients can run fixed update before the first full
// NetworkTransform update
m_Rigidbody.isKinematic = true;
SetIsKinematic(true);
}

private void SetIsKinematic(bool isKinematic)
{
#if UNITY_2022_3_OR_NEWER
m_Rigidbody.bodyType = isKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic;
#else
m_Rigidbody.isKinematic = isKinematic;
#endif
}

/// <summary>
Expand Down Expand Up @@ -89,7 +98,7 @@ private void UpdateOwnershipAuthority()
}

// If you have authority then you are not kinematic
m_Rigidbody.isKinematic = !m_IsAuthority;
SetIsKinematic(!m_IsAuthority);

// Set interpolation of the Rigidbody2D based on authority
// With authority: let local transform handle interpolation
Expand Down
22 changes: 8 additions & 14 deletions testproject/Assets/Tests/Runtime/Physics/NetworkRigidbodyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,21 @@ private void TestRigidBody2D()
Assert.True(serverClientPlayerNetworkRigidbody2d.WasKinematicBeforeSpawn);
Assert.True(clientServerPlayerNetworkRigidbody2d.WasKinematicBeforeSpawn);

// Validate kinematic settings after spawn
var serverLocalPlayerRigidbody2d = m_ServerNetworkManager.LocalClient.PlayerObject.GetComponent<Rigidbody2D>();
var clientLocalPlayerRigidbody2d = m_ClientNetworkManagers[0].LocalClient.PlayerObject.GetComponent<Rigidbody2D>();
var serverClientPlayerRigidbody2d = m_PlayerNetworkObjects[m_ServerNetworkManager.LocalClientId][m_ClientNetworkManagers[0].LocalClientId].GetComponent<Rigidbody2D>();
var clientServerPlayerRigidbody2d = m_PlayerNetworkObjects[m_ClientNetworkManagers[0].LocalClientId][m_ServerNetworkManager.LocalClientId].GetComponent<Rigidbody2D>();

var isOwnerAuthority = m_AuthorityMode == NetworkTransformRigidBodyTestComponent.AuthorityModes.Owner;
if (isOwnerAuthority)
{
// can commit player has authority and should have a kinematic mode of false (or true in case body was already kinematic).
Assert.True(!serverLocalPlayerRigidbody2d.isKinematic);
Assert.True(!clientLocalPlayerRigidbody2d.isKinematic);
Assert.True(serverClientPlayerRigidbody2d.isKinematic);
Assert.True(clientServerPlayerRigidbody2d.isKinematic);
Assert.True(!serverLocalPlayerNetworkRigidbody2d.IsKinematic());
Assert.True(!clientLocalPlayerNetworkRigidbody2d.IsKinematic());
Assert.True(serverClientPlayerNetworkRigidbody2d.IsKinematic());
Assert.True(clientServerPlayerNetworkRigidbody2d.IsKinematic());
}
else
{
Assert.True(!serverLocalPlayerRigidbody2d.isKinematic);
Assert.True(clientLocalPlayerRigidbody2d.isKinematic);
Assert.True(!serverClientPlayerRigidbody2d.isKinematic);
Assert.True(clientServerPlayerRigidbody2d.isKinematic);
Assert.True(!serverLocalPlayerNetworkRigidbody2d.IsKinematic());
Assert.True(clientLocalPlayerNetworkRigidbody2d.IsKinematic());
Assert.True(!serverClientPlayerNetworkRigidbody2d.IsKinematic());
Assert.True(clientServerPlayerNetworkRigidbody2d.IsKinematic());
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@ public class NetworkRigidbody2DTestComponent : NetworkRigidbody2D
{
public bool WasKinematicBeforeSpawn;


internal bool IsKinematic()
{
#if UNITY_2022_3_OR_NEWER
return GetComponent<Rigidbody2D>().bodyType == RigidbodyType2D.Kinematic;
#else
return GetComponent<Rigidbody2D>().isKinematic;
#endif
}

protected override void OnNetworkPreSpawn(ref NetworkManager networkManager)
{
WasKinematicBeforeSpawn = GetComponent<Rigidbody2D>().isKinematic;
WasKinematicBeforeSpawn = IsKinematic();
base.OnNetworkPreSpawn(ref networkManager);
}
}
Expand Down

0 comments on commit bd738b3

Please sign in to comment.