Class NetworkRigidbodyBase
NetworkRigidbodyBase is a unified Rigidbody and Rigidbody2D integration that helps to synchronize physics motion, collision, and interpolation when used with a NetworkTransform.
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(RigidbodyTypes, NetworkTransform, Rigidbody2D, Rigidbody) during instantiation (i.e. invoked from within the Awake method). You can re-initialize after having initialized but only when the NetworkObject is not spawned.
Fields
AutoSetKinematicOnDespawn
Primarily applies to the AutoUpdateKinematicState property when disabled but you still want the Rigidbody to be automatically set to Kinematic when despawned.
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 NetworkTransform will use the Rigidbody/Rigidbody2D to apply and synchronize changes in position, rotation, and allows for the use of Rigidbody interpolation/extrapolation.
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 |
---|---|
FixedJoint |
FixedJoint2D
Declaration
public FixedJoint2D FixedJoint2D { get; }
Property Value
Type | Description |
---|---|
FixedJoint2D |
RigidbodyType
Declaration
public NetworkRigidbodyBase.RigidbodyTypes RigidbodyType { get; }
Property Value
Type | Description |
---|---|
NetworkRigidbodyBase.RigidbodyTypes |
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 GameObject's Transform
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 UseRigidBodyForMotion enabled. Invoke this method on the rigid body you wish to attach to another (i.e. weapon to player, sticky bomb to player/object, etc). FixedJoint FixedJoint2D
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 |
---|---|---|
NetworkRigidbodyBase | 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 UseRigidBodyForMotion is enabled. For more details on using FixedJoint 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 NetworkRigidbodyBase.
DetachFromFixedJoint()
Authority Only: When invoked and already connected to an object via FixedJoint or FixedJoint2D (depending upon the type of rigid body), this will detach from the fixed joint and destroy the fixed joint component.
Declaration
public void DetachFromFixedJoint()
Remarks
This is the recommended way, as opposed to parenting, to attached/detatch two rigid bodies to one another when UseRigidBodyForMotion is enabled.
GetAngularVelocity()
Gets the angular velocity for the Rigidbody.
Declaration
public Vector3 GetAngularVelocity()
Returns
Type | Description |
---|---|
Vector3 | angular velocity as a Vector3 |
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
Type | Description |
---|---|
Vector3 | Vector3 as the linear velocity |
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
Type | Description |
---|---|
Vector3 |
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 NetworkRigidbodyBase.RigidbodyTypes passed in as a parameter.
Declaration
protected void Initialize(NetworkRigidbodyBase.RigidbodyTypes rigidbodyType, NetworkTransform networkTransform = null, Rigidbody2D rigidbody2D = null, Rigidbody rigidbody = null)
Parameters
Type | Name | Description |
---|---|---|
NetworkRigidbodyBase.RigidbodyTypes | rigidbodyType | type of rigid body being initialized |
NetworkTransform | networkTransform | |
Rigidbody2D | rigidbody2D | (optional) The Rigidbody2D to be used |
Rigidbody | rigidbody | (optional) The Rigidbody to be used |
Remarks
Cannot be initialized while the associated NetworkObject is spawned.
IsKinematic()
Declaration
public bool IsKinematic()
Returns
Type | Description |
---|---|
bool |
MovePosition(Vector3)
Moves the rigid body
Declaration
public void MovePosition(Vector3 position)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | position | The Vector3 position to move towards |
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 NetworkRigidbodyBase, this virtual method is invoked when the FixedJoint2D is created in the event any additional adjustments are needed.
Declaration
protected virtual void OnFixedJoint2DCreated()
OnFixedJointCreated()
When using a custom NetworkRigidbodyBase, this virtual method is invoked when the FixedJoint is created in the event any additional adjustments are needed.
Declaration
protected virtual void OnFixedJointCreated()
OnNetworkDespawn()
Gets called when the NetworkObject gets despawned. This method runs both client and server side.
Declaration
public override void OnNetworkDespawn()
Overrides
OnNetworkSpawn()
Gets called when the NetworkObject gets spawned, message handlers are ready to be registered, and the network is set up.
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
Type | Name | Description |
---|---|---|
ulong | previous | the previous owner |
ulong | current | the current owner |
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 NetworkTransform motion, this automatically adjusts from extrapolation to interpolation if:
- 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
Type | Name | Description |
---|---|---|
Vector3 | position | Vector3 position to apply to the Rigidbody |
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()