docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class NetworkRigidbodyBase

    NetworkRigidbodyBase is a unified Rigidbody and Rigidbody2D integration that helps to synchronize physics motion, collision, and interpolation when used with a NetworkTransform.

    Inheritance
    object
    Object
    Component
    Behaviour
    MonoBehaviour
    NetworkBehaviour
    NetworkRigidbodyBase
    NetworkRigidbody
    NetworkRigidbody2D
    Inherited Members
    NetworkBehaviour.NetworkManager
    NetworkBehaviour.RpcTarget
    NetworkBehaviour.IsLocalPlayer
    NetworkBehaviour.IsOwner
    NetworkBehaviour.IsServer
    NetworkBehaviour.HasAuthority
    NetworkBehaviour.IsSessionOwner
    NetworkBehaviour.ServerIsHost
    NetworkBehaviour.IsClient
    NetworkBehaviour.IsHost
    NetworkBehaviour.IsOwnedByServer
    NetworkBehaviour.IsSpawned
    NetworkBehaviour.NetworkObject
    NetworkBehaviour.HasNetworkObject
    NetworkBehaviour.NetworkObjectId
    NetworkBehaviour.NetworkBehaviourId
    NetworkBehaviour.GetNetworkBehaviour(ushort)
    NetworkBehaviour.OwnerClientId
    NetworkBehaviour.OnDeferringDespawn(int)
    NetworkBehaviour.OnNetworkPreSpawn(ref NetworkManager)
    NetworkBehaviour.OnNetworkPostSpawn()
    NetworkBehaviour.InternalOnNetworkPostSpawn()
    NetworkBehaviour.OnNetworkSessionSynchronized()
    NetworkBehaviour.InternalOnNetworkSessionSynchronized()
    NetworkBehaviour.OnInSceneObjectsSpawned()
    NetworkBehaviour.OnGainedOwnership()
    NetworkBehaviour.OnLostOwnership()
    NetworkBehaviour.OnNetworkObjectParentChanged(NetworkObject)
    NetworkBehaviour.GetNetworkObject(ulong)
    NetworkBehaviour.OnSynchronize<T>(ref BufferSerializer<T>)
    NetworkBehaviour.OnReanticipate(double)
    NetworkBehaviour.m_TargetIdBeingSynchronized
    NetworkBehaviour.OnDestroy()
    MonoBehaviour.IsInvoking()
    MonoBehaviour.CancelInvoke()
    MonoBehaviour.Invoke(string, float)
    MonoBehaviour.InvokeRepeating(string, float, float)
    MonoBehaviour.CancelInvoke(string)
    MonoBehaviour.IsInvoking(string)
    MonoBehaviour.StartCoroutine(string)
    MonoBehaviour.StartCoroutine(string, object)
    MonoBehaviour.StartCoroutine(IEnumerator)
    MonoBehaviour.StartCoroutine_Auto(IEnumerator)
    MonoBehaviour.StopCoroutine(IEnumerator)
    MonoBehaviour.StopCoroutine(Coroutine)
    MonoBehaviour.StopCoroutine(string)
    MonoBehaviour.StopAllCoroutines()
    MonoBehaviour.print(object)
    MonoBehaviour.destroyCancellationToken
    MonoBehaviour.useGUILayout
    MonoBehaviour.didStart
    MonoBehaviour.didAwake
    MonoBehaviour.runInEditMode
    Behaviour.enabled
    Behaviour.isActiveAndEnabled
    Component.GetComponent(Type)
    Component.GetComponent<T>()
    Component.TryGetComponent(Type, out Component)
    Component.TryGetComponent<T>(out T)
    Component.GetComponent(string)
    Component.GetComponentInChildren(Type, bool)
    Component.GetComponentInChildren(Type)
    Component.GetComponentInChildren<T>(bool)
    Component.GetComponentInChildren<T>()
    Component.GetComponentsInChildren(Type, bool)
    Component.GetComponentsInChildren(Type)
    Component.GetComponentsInChildren<T>(bool)
    Component.GetComponentsInChildren<T>(bool, List<T>)
    Component.GetComponentsInChildren<T>()
    Component.GetComponentsInChildren<T>(List<T>)
    Component.GetComponentInParent(Type, bool)
    Component.GetComponentInParent(Type)
    Component.GetComponentInParent<T>(bool)
    Component.GetComponentInParent<T>()
    Component.GetComponentsInParent(Type, bool)
    Component.GetComponentsInParent(Type)
    Component.GetComponentsInParent<T>(bool)
    Component.GetComponentsInParent<T>(bool, List<T>)
    Component.GetComponentsInParent<T>()
    Component.GetComponents(Type)
    Component.GetComponents(Type, List<Component>)
    Component.GetComponents<T>(List<T>)
    Component.GetComponents<T>()
    Component.GetComponentIndex()
    Component.CompareTag(string)
    Component.CompareTag(TagHandle)
    Component.SendMessageUpwards(string, object, SendMessageOptions)
    Component.SendMessageUpwards(string, object)
    Component.SendMessageUpwards(string)
    Component.SendMessageUpwards(string, SendMessageOptions)
    Component.SendMessage(string, object)
    Component.SendMessage(string)
    Component.SendMessage(string, object, SendMessageOptions)
    Component.SendMessage(string, SendMessageOptions)
    Component.BroadcastMessage(string, object, SendMessageOptions)
    Component.BroadcastMessage(string, object)
    Component.BroadcastMessage(string)
    Component.BroadcastMessage(string, SendMessageOptions)
    Component.transform
    Component.gameObject
    Component.tag
    Object.GetInstanceID()
    Object.GetHashCode()
    Object.Equals(object)
    Object.InstantiateAsync<T>(T)
    Object.InstantiateAsync<T>(T, Transform)
    Object.InstantiateAsync<T>(T, Vector3, Quaternion)
    Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)
    Object.InstantiateAsync<T>(T, int)
    Object.InstantiateAsync<T>(T, int, Transform)
    Object.InstantiateAsync<T>(T, int, Vector3, Quaternion)
    Object.InstantiateAsync<T>(T, int, ReadOnlySpan<Vector3>, ReadOnlySpan<Quaternion>)
    Object.InstantiateAsync<T>(T, int, Transform, Vector3, Quaternion)
    Object.InstantiateAsync<T>(T, int, Transform, Vector3, Quaternion, CancellationToken)
    Object.InstantiateAsync<T>(T, int, Transform, ReadOnlySpan<Vector3>, ReadOnlySpan<Quaternion>)
    Object.InstantiateAsync<T>(T, int, Transform, ReadOnlySpan<Vector3>, ReadOnlySpan<Quaternion>, CancellationToken)
    Object.Instantiate(Object, Vector3, Quaternion)
    Object.Instantiate(Object, Vector3, Quaternion, Transform)
    Object.Instantiate(Object)
    Object.Instantiate(Object, Scene)
    Object.Instantiate(Object, Transform)
    Object.Instantiate(Object, Transform, bool)
    Object.Instantiate<T>(T)
    Object.Instantiate<T>(T, Vector3, Quaternion)
    Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
    Object.Instantiate<T>(T, Transform)
    Object.Instantiate<T>(T, Transform, bool)
    Object.Destroy(Object, float)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, bool)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.FindObjectsOfType(Type, bool)
    Object.FindObjectsByType(Type, FindObjectsSortMode)
    Object.FindObjectsByType(Type, FindObjectsInactive, FindObjectsSortMode)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, float)
    Object.DestroyObject(Object)
    Object.FindSceneObjectsOfType(Type)
    Object.FindObjectsOfTypeIncludingAssets(Type)
    Object.FindObjectsOfType<T>()
    Object.FindObjectsByType<T>(FindObjectsSortMode)
    Object.FindObjectsOfType<T>(bool)
    Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)
    Object.FindObjectOfType<T>()
    Object.FindObjectOfType<T>(bool)
    Object.FindFirstObjectByType<T>()
    Object.FindAnyObjectByType<T>()
    Object.FindFirstObjectByType<T>(FindObjectsInactive)
    Object.FindAnyObjectByType<T>(FindObjectsInactive)
    Object.FindObjectsOfTypeAll(Type)
    Object.FindObjectOfType(Type)
    Object.FindFirstObjectByType(Type)
    Object.FindAnyObjectByType(Type)
    Object.FindObjectOfType(Type, bool)
    Object.FindFirstObjectByType(Type, FindObjectsInactive)
    Object.FindAnyObjectByType(Type, FindObjectsInactive)
    Object.ToString()
    Object.name
    Object.hideFlags
    object.Equals(object, object)
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    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

    Vector3

    GetRotation()

    Gets the rotation of the Rigidbody

    Declaration
    public Quaternion GetRotation()
    Returns
    Type Description
    Quaternion

    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
    NetworkBehaviour.OnNetworkDespawn()

    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
    NetworkBehaviour.OnNetworkSpawn()

    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
    NetworkBehaviour.OnOwnershipChanged(ulong, ulong)

    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()
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)