Class NetworkRigidbodyBase
NetworkRigidbodyBase is a unified Rigidbody and Rigidbody2D integration that helps to synchronize physics motion, collision, and interpolation
when used with a Network
Inherited Members
Namespace: Unity.Netcode.Components
Assembly: Unity.Netcode.Runtime.dll
Syntax
public abstract class NetworkRigidbodyBase : NetworkBehaviour
Remarks
For a customizable netcode Rigidbody, create your own component from this class and use Initialize(Rigidbody
Fields
AutoSetKinematicOnDespawn
Primarily applies to the Auto
Declaration
public bool AutoSetKinematicOnDespawn
Field Value
Type | Description |
---|---|
bool |
AutoUpdateKinematicState
When enabled (default), automatically set the Kinematic state of the Rigidbody based on ownership. When disabled, Kinematic state needs to be set by external script(s).
Declaration
public bool AutoUpdateKinematicState
Field Value
Type | Description |
---|---|
bool |
UseRigidBodyForMotion
When enabled, the associated Network
Declaration
[Tooltip("When enabled and a NetworkTransform component is attached, the NetworkTransform will use the rigid body for motion and detecting changes in state.")]
public bool UseRigidBodyForMotion
Field Value
Type | Description |
---|---|
bool |
Remarks
If Interpolate is enabled, non-authoritative instances can only use Rigidbody interpolation. If a network prefab is set to extrapolation and Interpolate is enabled, then non-authoritative instances will automatically be adjusted to use Rigidbody interpolation while the authoritative instance will still use extrapolation.
Properties
FixedJoint
Declaration
public FixedJoint FixedJoint { get; }
Property Value
Type | Description |
---|---|
Fixed |
FixedJoint2D
Declaration
public FixedJoint2D FixedJoint2D { get; }
Property Value
Type | Description |
---|---|
Fixed |
RigidbodyType
Declaration
public NetworkRigidbodyBase.RigidbodyTypes RigidbodyType { get; }
Property Value
Type | Description |
---|---|
Network |
m_InternalRigidbody
Declaration
protected Rigidbody m_InternalRigidbody { get; }
Property Value
Type | Description |
---|---|
Rigidbody |
m_InternalRigidbody2D
Declaration
protected Rigidbody2D m_InternalRigidbody2D { get; }
Property Value
Type | Description |
---|---|
Rigidbody2D |
Methods
ApplyCurrentTransform()
Applies the rotation and position of the Game
Declaration
public void ApplyCurrentTransform()
AttachToFixedJoint(NetworkRigidbodyBase, Vector3, float, float, bool, bool, bool)
Authority Only:
When invoked and not already attached to a fixed joint, this will connect two rigid bodies with Use
Declaration
public bool AttachToFixedJoint(NetworkRigidbodyBase objectToConnectTo, Vector3 positionOfConnection, float connectedMassScale = 0, float massScale = 1, bool useGravity = false, bool zeroVelocity = true, bool teleportObject = true)
Parameters
Type | Name | Description |
---|---|---|
Network |
objectToConnectTo | The target object to attach to. |
Vector3 | positionOfConnection | The position of the connection (i.e. where you want the object to be affixed). |
float | connectedMassScale | The target object's mass scale relative to this object being attached. |
float | massScale | This object's mass scale relative to the target object's. |
bool | useGravity | Determines if this object will have gravity applied to it along with the object you are connecting this one to (the default is to not use gravity for this object) |
bool | zeroVelocity | When true (the default), both linear and angular velocities of this object are set to zero. |
bool | teleportObject | When true (the default), this object will teleport itself to the position of connection. |
Returns
Type | Description |
---|---|
bool | true (success) false (failed) |
Remarks
Parenting relative:
- This instance can be viewed as the child.
- The can be viewed as the parent.
This is the recommended way, as opposed to parenting, to attached/detatch two rigid bodies to one another when UseRigid is enabled. For more details on using FixedBody For Motion Joint and FixedJoint2D .
This provides a simple joint solution between two rigid bodies and serves as an example. You can add different joint types by creating a customized/derived version of NetworkRigidbody .Base
DetachFromFixedJoint()
Authority Only:
When invoked and already connected to an object via Fixed
Declaration
public void DetachFromFixedJoint()
Remarks
This is the recommended way, as opposed to parenting, to attached/detatch two rigid bodies to one another when Use
GetAngularVelocity()
Gets the angular velocity for the Rigidbody.
Declaration
public Vector3 GetAngularVelocity()
Returns
Remarks
For Rigidbody2D, the z component of the Vector3 returned is the angular velocity of the object.
GetLinearVelocity()
Gets the linear velocity of the Rigidbody.
Declaration
public Vector3 GetLinearVelocity()
Returns
Remarks
For Rigidbody2D, the Vector3 velocity returned is only applied to the x and y components.
GetPosition()
Gets the position of the Rigidbody
Declaration
public Vector3 GetPosition()
Returns
GetRotation()
Gets the rotation of the Rigidbody
Declaration
public Quaternion GetRotation()
Returns
Type | Description |
---|---|
Quaternion |
Initialize(RigidbodyTypes, NetworkTransform, Rigidbody2D, Rigidbody)
Initializes the networked Rigidbody based on the Network
Declaration
protected void Initialize(NetworkRigidbodyBase.RigidbodyTypes rigidbodyType, NetworkTransform networkTransform = null, Rigidbody2D rigidbody2D = null, Rigidbody rigidbody = null)
Parameters
Type | Name | Description |
---|---|---|
Network |
rigidbodyType | type of rigid body being initialized |
Network |
networkTransform | |
Rigidbody2D | rigidbody2D | (optional) The Rigidbody2D to be used |
Rigidbody | rigidbody | (optional) The Rigidbody to be used |
Remarks
Cannot be initialized while the associated Network
IsKinematic()
Declaration
public bool IsKinematic()
Returns
Type | Description |
---|---|
bool |
MovePosition(Vector3)
Moves the rigid body
Declaration
public void MovePosition(Vector3 position)
Parameters
MoveRotation(Quaternion)
Rotatates the Rigidbody towards a specified rotation
Declaration
public void MoveRotation(Quaternion rotation)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | rotation | The rotation expressed as a Quaternion |
OnFixedJoint2DCreated()
When using a custom Network
Declaration
protected virtual void OnFixedJoint2DCreated()
OnFixedJointCreated()
When using a custom Network
Declaration
protected virtual void OnFixedJointCreated()
OnNetworkDespawn()
Gets called when the Network
Declaration
public override void OnNetworkDespawn()
Overrides
OnNetworkSpawn()
Gets called when the Network
Declaration
public override void OnNetworkSpawn()
Overrides
OnOwnershipChanged(ulong, ulong)
Invoked on all clients. Override this method to be notified of any ownership changes (even if the instance was neither the previous or newly assigned current owner).
Declaration
protected override void OnOwnershipChanged(ulong previous, ulong current)
Parameters
Overrides
ResetInterpolation()
Declaration
public void ResetInterpolation()
SetAngularVelocity(Vector3)
Sets the angular velocity for the Rigidbody.
Declaration
public void SetAngularVelocity(Vector3 angularVelocity)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | angularVelocity | the angular velocity to apply to the body |
Remarks
For Rigidbody2D, the z component of is only used to set the angular velocity. A quick way to pass in a 2D angular velocity component is: forward * angularVelocity (where angularVelocity is a float)
SetIsKinematic(bool)
Sets the kinematic state of the Rigidbody and handles updating the Rigidbody's interpolation setting based on the Kinematic state.
Declaration
public void SetIsKinematic(bool isKinematic)
Parameters
Type | Name | Description |
---|---|---|
bool | isKinematic |
Remarks
When using the Rigidbody for Network
- The Rigidbody was originally set to extrapolation
- The NetworkTransform is set to interpolate When the two above conditions are true:
- When switching from non-kinematic to kinematic this will automatically switch the Rigidbody from extrapolation to interpolate.
- When switching from kinematic to non-kinematic this will automatically switch the Rigidbody from interpolation back to extrapolation.
SetLinearVelocity(Vector3)
Sets the linear velocity of the Rigidbody.
Declaration
public void SetLinearVelocity(Vector3 linearVelocity)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | linearVelocity |
Remarks
For Rigidbody2D, only the x and y components of the Vector3 are applied.
SetPosition(Vector3)
Directly applies a position (like teleporting)
Declaration
public void SetPosition(Vector3 position)
Parameters
SetRotation(Quaternion)
Applies a rotation to the Rigidbody
Declaration
public void SetRotation(Quaternion rotation)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | rotation | The rotation to apply expressed as a Quaternion |
SleepRigidbody()
Puts the Rigidbody to sleep
Declaration
public void SleepRigidbody()
WakeIfSleeping()
Wakes the Rigidbody if it is sleeping
Declaration
public void WakeIfSleeping()