Version: 2022.2
LanguageEnglish
  • C#

Rigidbody2D

class in UnityEngine

/

Inherits from:Component

/

Implemented in:UnityEngine.Physics2DModule

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Description

Rigidbody physics component for 2D sprites.

The Rigidbody2D class essentially provides the same functionality in 2D that the Rigidbody class provides in 3D. Adding a Rigidbody2D component to a sprite puts it under the control of the physics engine. By itself, this means that the sprite will be affected by gravity and can be controlled from scripts using forces. By adding the appropriate collider component, the sprite will also respond to collisions with other sprites. This behaviour comes entirely from Unity's physics system; very little code is required to get impressive and authentic physical behaviour and allows for "emergent" gameplay that was not explicitly coded into the game.

See Also: Rigidbody class, SpriteRenderer class, Collider2D class, Joint2D class.

Properties

angularDragCoefficient of angular drag.
angularVelocityAngular velocity in degrees per second.
attachedColliderCountReturns the number of Collider2D attached to this Rigidbody2D.
bodyTypeThe physical behaviour type of the Rigidbody2D.
centerOfMassThe center of mass of the rigidBody in local space.
collisionDetectionModeThe method used by the physics engine to check if two objects have collided.
constraintsControls which degrees of freedom are allowed for the simulation of this Rigidbody2D.
dragCoefficient of drag.
excludeLayersThe additional Layers that all Collider2D attached to this Rigidbody2D should exclude when deciding if a contact with another Collider2D should happen or not.
freezeRotationControls whether physics will change the rotation of the object.
gravityScaleThe degree to which this object is affected by gravity.
includeLayersThe additional Layers that all Collider2D attached to this Rigidbody2D should include when deciding if a contact with another Collider2D should happen or not.
inertiaThe Rigidbody's resistance to changes in angular velocity (rotation).
interpolationPhysics interpolation used between updates.
isKinematicShould this rigidbody be taken out of physics control?
massMass of the Rigidbody.
positionThe position of the rigidbody.
rotationThe rotation of the rigidbody.
sharedMaterialThe PhysicsMaterial2D that is applied to all Collider2D attached to this Rigidbody2D.
simulatedIndicates whether the rigid body should be simulated or not by the physics system.
sleepModeThe sleep state that the rigidbody will initially be in.
totalForceThe total amount of force that has been explicitly applied to this Rigidbody2D since the last physics simulation step.
totalTorqueThe total amount of torque that has been explicitly applied to this Rigidbody2D since the last physics simulation step.
useAutoMassShould the total rigid-body mass be automatically calculated from the [[Collider2D.density]] of attached colliders?
useFullKinematicContactsShould kinematic/kinematic and kinematic/static collisions be allowed?
velocityLinear velocity of the Rigidbody in units per second.
worldCenterOfMassGets the center of mass of the rigidBody in global space.

Public Methods

AddForceApply a force to the rigidbody.
AddForceAtPositionApply a force at a given position in space.
AddRelativeForceAdds a force to the rigidbody2D relative to its coordinate system.
AddTorqueApply a torque at the rigidbody's centre of mass.
CastAll the Collider2D shapes attached to the Rigidbody2D are cast into the Scene starting at each Collider position ignoring the Colliders attached to the same Rigidbody2D.
ClosestPointReturns a point on the perimeter of all enabled Colliders attached to this Rigidbody that is closest to the specified position.
DistanceCalculates the minimum distance of this collider against all Collider2D attached to this Rigidbody2D.
GetAttachedCollidersReturns all Collider2D that are attached to this Rigidbody2D.
GetContactsRetrieves all contact points for all of the Collider(s) attached to this Rigidbody.
GetPointGet a local space point given the point point in rigidBody global space.
GetPointVelocityThe velocity of the rigidbody at the point Point in global space.
GetRelativePointGet a global space point given the point relativePoint in rigidBody local space.
GetRelativePointVelocityThe velocity of the rigidbody at the point Point in local space.
GetRelativeVectorGet a global space vector given the vector relativeVector in rigidBody local space.
GetShapesGets all the PhysicsShape2D used by all Collider2D attached to the Rigidbody2D.
GetVectorGet a local space vector given the vector vector in rigidBody global space.
IsAwakeIs the rigidbody "awake"?
IsSleepingIs the rigidbody "sleeping"?
IsTouchingChecks whether the collider is touching any of the collider(s) attached to this rigidbody or not.
IsTouchingLayersChecks whether any of the collider(s) attached to this rigidbody are touching any colliders on the specified layerMask or not.
MovePositionMoves the rigidbody to position.
MoveRotationRotates the Rigidbody to angle (given in degrees).
OverlapColliderGet a list of all Colliders that overlap all Colliders attached to this Rigidbody2D.
OverlapPointCheck if any of the Rigidbody2D colliders overlap a point in space.
SetRotationSets the rotation of the Rigidbody2D to angle (given in degrees).
SleepMake the rigidbody "sleep".
WakeUpDisables the "sleeping" state of a rigidbody.

Inherited Members

Properties

gameObjectThe game object this component is attached to. A component is always attached to a game object.
tagThe tag of this game object.
transformThe Transform attached to this GameObject.
hideFlagsShould the object be hidden, saved with the Scene or modifiable by the user?
nameThe name of the object.

Public Methods

BroadcastMessageCalls the method named methodName on every MonoBehaviour in this game object or any of its children.
CompareTagChecks the GameObject's tag against the defined tag.
GetComponentGets a reference to a component of type T on the same GameObject as the component specified.
GetComponentInChildrenGets a reference to a component of type T on the same GameObject as the component specified, or any child of the GameObject.
GetComponentInParentGets a reference to a component of type T on the same GameObject as the component specified, or any parent of the GameObject.
GetComponentsGets references to all components of type T on the same GameObject as the component specified.
GetComponentsInChildrenGets references to all components of type T on the same GameObject as the component specified, and any child of the GameObject.
GetComponentsInParentGets references to all components of type T on the same GameObject as the component specified, and any parent of the GameObject.
SendMessageCalls the method named methodName on every MonoBehaviour in this game object.
SendMessageUpwardsCalls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour.
TryGetComponentGets the component of the specified type, if it exists.
GetInstanceIDGets the instance ID of the object.
ToStringReturns the name of the object.

Static Methods

DestroyRemoves a GameObject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadDo not destroy the target Object when loading a new Scene.
FindAnyObjectByTypeRetrieves any active loaded object of Type type.
FindFirstObjectByTypeRetrieves the first active loaded object of Type type.
FindObjectOfTypeReturns the first active loaded object of Type type.
FindObjectsByTypeRetrieves a list of all loaded objects of Type type.
FindObjectsOfTypeGets a list of all loaded objects of Type type.
InstantiateClones the object original and returns the clone.

Operators

boolDoes the object exist?
operator !=Compares if two objects refer to a different object.
operator ==Compares two object references to see if they refer to the same object.