Skip to content

Commit bcb891b

Browse files
fix: NetworkRigidbody2D not changing body type with authority [MTT-8510] (#2916)
* fix This resolves the issue with Rigidbody2d not changing the body type based on changes to authority while also assuring the initial body type for Rigidbody2d is kinematic until spawned. * test Realized we needed an overhaul with our rigid body tests so I did a bit of refactoring. Deleted the remaining of the completely commented out rigid body tests that are now being covered in the NetworkRigidbodyTests.
1 parent cd6ead3 commit bcb891b

13 files changed

+289
-409
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1616

1717
### Fixed
1818

19+
- Fixed issue where `NetworkRigidbody2D` would not properly change body type based on the instance's authority when spawned. (#2916)
1920
- Fixed issue where a `NetworkObject` component's associated `NetworkBehaviour` components would not be detected if scene loading is disabled in the editor and the currently loaded scene has in-scene placed `NetworkObject`s. (#2906)
2021

2122
### Changed

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

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,76 +12,101 @@ namespace Unity.Netcode.Components
1212
[AddComponentMenu("Netcode/Network Rigidbody 2D")]
1313
public class NetworkRigidbody2D : NetworkBehaviour
1414
{
15+
/// <summary>
16+
/// Determines if we are server (true) or owner (false) authoritative
17+
/// </summary>
18+
private bool m_IsServerAuthoritative;
19+
1520
private Rigidbody2D m_Rigidbody;
1621
private NetworkTransform m_NetworkTransform;
1722

18-
private bool m_OriginalKinematic;
1923
private RigidbodyInterpolation2D m_OriginalInterpolation;
2024

2125
// Used to cache the authority state of this rigidbody during the last frame
2226
private bool m_IsAuthority;
2327

28+
private void Awake()
29+
{
30+
m_NetworkTransform = GetComponent<NetworkTransform>();
31+
m_IsServerAuthoritative = m_NetworkTransform.IsServerAuthoritative();
32+
33+
SetupRigidBody();
34+
}
35+
2436
/// <summary>
25-
/// Gets a bool value indicating whether this <see cref="NetworkRigidbody2D"/> on this peer currently holds authority.
37+
/// If the current <see cref="NetworkTransform"/> has authority,
38+
/// then use the <see cref="Rigidbody2D"/> interpolation strategy,
39+
/// if the <see cref="NetworkTransform"/> is handling interpolation,
40+
/// set interpolation to none on the <see cref="Rigidbody2D"/>
41+
/// <br/>
42+
/// Turn off physics for the rigid body until spawned, otherwise
43+
/// clients can run fixed update before the first
44+
/// full <see cref="NetworkTransform"/> update
2645
/// </summary>
27-
private bool HasAuthority => m_NetworkTransform.CanCommitToTransform;
28-
29-
private void Awake()
46+
private void SetupRigidBody()
3047
{
3148
m_Rigidbody = GetComponent<Rigidbody2D>();
32-
m_NetworkTransform = GetComponent<NetworkTransform>();
49+
m_OriginalInterpolation = m_Rigidbody.interpolation;
3350

51+
m_Rigidbody.interpolation = m_IsAuthority ? m_OriginalInterpolation : (m_NetworkTransform.Interpolate ? RigidbodyInterpolation2D.None : m_OriginalInterpolation);
3452
// Turn off physics for the rigid body until spawned, otherwise
3553
// clients can run fixed update before the first full
3654
// NetworkTransform update
3755
m_Rigidbody.isKinematic = true;
3856
}
3957

40-
private void FixedUpdate()
58+
/// <summary>
59+
/// For owner authoritative (i.e. ClientNetworkTransform)
60+
/// we adjust our authority when we gain ownership
61+
/// </summary>
62+
public override void OnGainedOwnership()
63+
{
64+
UpdateOwnershipAuthority();
65+
}
66+
67+
/// <summary>
68+
/// For owner authoritative(i.e. ClientNetworkTransform)
69+
/// we adjust our authority when we have lost ownership
70+
/// </summary>
71+
public override void OnLostOwnership()
4172
{
42-
if (IsSpawned)
43-
{
44-
if (HasAuthority != m_IsAuthority)
45-
{
46-
m_IsAuthority = HasAuthority;
47-
UpdateRigidbodyKinematicMode();
48-
}
49-
}
73+
UpdateOwnershipAuthority();
5074
}
5175

52-
// Puts the rigidbody in a kinematic non-interpolated mode on everyone but the server.
53-
private void UpdateRigidbodyKinematicMode()
76+
/// <summary>
77+
/// Sets the authority differently depending upon
78+
/// whether it is server or owner authoritative
79+
/// </summary>
80+
private void UpdateOwnershipAuthority()
5481
{
55-
if (m_IsAuthority == false)
82+
if (m_IsServerAuthoritative)
5683
{
57-
m_OriginalKinematic = m_Rigidbody.isKinematic;
58-
m_Rigidbody.isKinematic = true;
59-
60-
m_OriginalInterpolation = m_Rigidbody.interpolation;
61-
// Set interpolation to none, the NetworkTransform component interpolates the position of the object.
62-
m_Rigidbody.interpolation = RigidbodyInterpolation2D.None;
84+
m_IsAuthority = NetworkManager.IsServer;
6385
}
6486
else
6587
{
66-
// Resets the rigidbody back to it's non replication only state. Happens on shutdown and when authority is lost
67-
m_Rigidbody.isKinematic = m_OriginalKinematic;
68-
m_Rigidbody.interpolation = m_OriginalInterpolation;
88+
m_IsAuthority = IsOwner;
6989
}
90+
91+
// If you have authority then you are not kinematic
92+
m_Rigidbody.isKinematic = !m_IsAuthority;
93+
94+
// Set interpolation of the Rigidbody2D based on authority
95+
// With authority: let local transform handle interpolation
96+
// Without authority: let the NetworkTransform handle interpolation
97+
m_Rigidbody.interpolation = m_IsAuthority ? m_OriginalInterpolation : RigidbodyInterpolation2D.None;
7098
}
7199

72100
/// <inheritdoc />
73101
public override void OnNetworkSpawn()
74102
{
75-
m_IsAuthority = HasAuthority;
76-
m_OriginalKinematic = m_Rigidbody.isKinematic;
77-
m_OriginalInterpolation = m_Rigidbody.interpolation;
78-
UpdateRigidbodyKinematicMode();
103+
UpdateOwnershipAuthority();
79104
}
80105

81106
/// <inheritdoc />
82107
public override void OnNetworkDespawn()
83108
{
84-
UpdateRigidbodyKinematicMode();
109+
UpdateOwnershipAuthority();
85110
}
86111
}
87112
}

testproject/Assets/Tests/Runtime/Physics/NetworkRigidbody2DCntChangeOwnershipTest.cs

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

testproject/Assets/Tests/Runtime/Physics/NetworkRigidbody2DCntChangeOwnershipTest.cs.meta

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

testproject/Assets/Tests/Runtime/Physics/NetworkRigidbody2DCntTest.cs

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

0 commit comments

Comments
 (0)