#if COM_UNITY_MODULES_PHYSICS2D
using UnityEngine;
namespace Unity.Netcode.Components
{
///
/// NetworkRigidbody allows for the use of on network objects. By controlling the kinematic
/// mode of the rigidbody and disabling it on all peers but the authoritative one.
///
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(NetworkTransform))]
[AddComponentMenu("Netcode/Network Rigidbody 2D")]
public class NetworkRigidbody2D : NetworkBehaviour
{
private Rigidbody2D m_Rigidbody;
private NetworkTransform m_NetworkTransform;
private bool m_OriginalKinematic;
private RigidbodyInterpolation2D m_OriginalInterpolation;
// Used to cache the authority state of this rigidbody during the last frame
private bool m_IsAuthority;
///
/// Gets a bool value indicating whether this on this peer currently holds authority.
///
private bool HasAuthority => m_NetworkTransform.CanCommitToTransform;
private void Awake()
{
m_Rigidbody = GetComponent();
m_NetworkTransform = GetComponent();
}
private void FixedUpdate()
{
if (IsSpawned)
{
if (HasAuthority != m_IsAuthority)
{
m_IsAuthority = HasAuthority;
UpdateRigidbodyKinematicMode();
}
}
}
// Puts the rigidbody in a kinematic non-interpolated mode on everyone but the server.
private void UpdateRigidbodyKinematicMode()
{
if (m_IsAuthority == false)
{
m_OriginalKinematic = m_Rigidbody.isKinematic;
m_Rigidbody.isKinematic = true;
m_OriginalInterpolation = m_Rigidbody.interpolation;
// Set interpolation to none, the NetworkTransform component interpolates the position of the object.
m_Rigidbody.interpolation = RigidbodyInterpolation2D.None;
}
else
{
// Resets the rigidbody back to it's non replication only state. Happens on shutdown and when authority is lost
m_Rigidbody.isKinematic = m_OriginalKinematic;
m_Rigidbody.interpolation = m_OriginalInterpolation;
}
}
///
public override void OnNetworkSpawn()
{
m_IsAuthority = HasAuthority;
m_OriginalKinematic = m_Rigidbody.isKinematic;
m_OriginalInterpolation = m_Rigidbody.interpolation;
UpdateRigidbodyKinematicMode();
}
///
public override void OnNetworkDespawn()
{
UpdateRigidbodyKinematicMode();
}
}
}
#endif // COM_UNITY_MODULES_PHYSICS2D