#if COM_UNITY_MODULES_PHYSICS
using UnityEngine;
namespace Unity.Netcode.Components
{
///
/// NetworkRigidbody allows for the use of on network objects. By controlling the kinematic
/// mode of the and disabling it on all peers but the authoritative one.
///
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(NetworkTransform))]
[AddComponentMenu("Netcode/Network Rigidbody")]
public class NetworkRigidbody : NetworkBehaviour
{
///
/// Determines if we are server (true) or owner (false) authoritative
///
private bool m_IsServerAuthoritative;
private Rigidbody m_Rigidbody;
private NetworkTransform m_NetworkTransform;
private RigidbodyInterpolation m_OriginalInterpolation;
// Used to cache the authority state of this Rigidbody during the last frame
private bool m_IsAuthority;
private void Awake()
{
m_NetworkTransform = GetComponent();
m_IsServerAuthoritative = m_NetworkTransform.IsServerAuthoritative();
m_Rigidbody = GetComponent();
m_OriginalInterpolation = m_Rigidbody.interpolation;
// Set interpolation to none if NetworkTransform is handling interpolation, otherwise it sets it to the original value
m_Rigidbody.interpolation = m_NetworkTransform.Interpolate ? RigidbodyInterpolation.None : m_OriginalInterpolation;
// 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;
}
///
/// For owner authoritative (i.e. ClientNetworkTransform)
/// we adjust our authority when we gain ownership
///
public override void OnGainedOwnership()
{
UpdateOwnershipAuthority();
}
///
/// For owner authoritative(i.e. ClientNetworkTransform)
/// we adjust our authority when we have lost ownership
///
public override void OnLostOwnership()
{
UpdateOwnershipAuthority();
}
///
/// Sets the authority differently depending upon
/// whether it is server or owner authoritative
///
private void UpdateOwnershipAuthority()
{
if (m_IsServerAuthoritative)
{
m_IsAuthority = NetworkManager.IsServer;
}
else
{
m_IsAuthority = IsOwner;
}
// If you have authority then you are not kinematic
m_Rigidbody.isKinematic = !m_IsAuthority;
// Set interpolation of the Rigidbody based on authority
// With authority: let local transform handle interpolation
// Without authority: let the NetworkTransform handle interpolation
m_Rigidbody.interpolation = m_IsAuthority ? m_OriginalInterpolation : RigidbodyInterpolation.None;
}
///
public override void OnNetworkSpawn()
{
UpdateOwnershipAuthority();
}
///
public override void OnNetworkDespawn()
{
m_Rigidbody.interpolation = m_OriginalInterpolation;
// Turn off physics for the rigid body until spawned, otherwise
// non-owners can run fixed updates before the first full
// NetworkTransform update and physics will be applied (i.e. gravity, etc)
m_Rigidbody.isKinematic = true;
}
}
}
#endif // COM_UNITY_MODULES_PHYSICS