Skip to content

Commit 7a2f31a

Browse files
authored
fix: Freeze rigidbody on Awake and interpolate based on ownership (#2838)
NetworkRigidbody is not checking for NetworkTransform.Interpolate value on ownership loss as it does on Awake, so it assumes that the NetworkTransform will treat the interpolation even if NetworkTransform.Interpolate is false. NetworkRigibody set kinematic on a NetworkObject's rigidbody before it actually is spawned (on Awake) to prevent the transform from being changed before knowing the actual ownership of the object. NetworkRigidbody2D follow the same pattern
1 parent c013f64 commit 7a2f31a

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,25 @@ private void Awake()
2929
m_NetworkTransform = GetComponent<NetworkTransform>();
3030
m_IsServerAuthoritative = m_NetworkTransform.IsServerAuthoritative();
3131

32+
SetupRigidBody();
33+
}
34+
35+
/// <summary>
36+
/// If the current <see cref="NetworkTransform"/> has authority,
37+
/// then use the <see cref="RigidBody"/> interpolation strategy,
38+
/// if the <see cref="NetworkTransform"/> is handling interpolation,
39+
/// set interpolation to none on the <see cref="RigidBody"/>
40+
/// <br/>
41+
/// Turn off physics for the rigid body until spawned, otherwise
42+
/// clients can run fixed update before the first
43+
/// full <see cref="NetworkTransform"/> update
44+
/// </summary>
45+
private void SetupRigidBody()
46+
{
3247
m_Rigidbody = GetComponent<Rigidbody>();
3348
m_OriginalInterpolation = m_Rigidbody.interpolation;
3449

35-
// Set interpolation to none if NetworkTransform is handling interpolation, otherwise it sets it to the original value
36-
m_Rigidbody.interpolation = m_NetworkTransform.Interpolate ? RigidbodyInterpolation.None : m_OriginalInterpolation;
37-
38-
// Turn off physics for the rigid body until spawned, otherwise
39-
// clients can run fixed update before the first full
40-
// NetworkTransform update
50+
m_Rigidbody.interpolation = m_IsAuthority ? m_OriginalInterpolation : (m_NetworkTransform.Interpolate ? RigidbodyInterpolation.None : m_OriginalInterpolation);
4151
m_Rigidbody.isKinematic = true;
4252
}
4353

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ private void Awake()
3030
{
3131
m_Rigidbody = GetComponent<Rigidbody2D>();
3232
m_NetworkTransform = GetComponent<NetworkTransform>();
33+
34+
// Turn off physics for the rigid body until spawned, otherwise
35+
// clients can run fixed update before the first full
36+
// NetworkTransform update
37+
m_Rigidbody.isKinematic = true;
3338
}
3439

3540
private void FixedUpdate()

0 commit comments

Comments
 (0)