Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Rigidbody2D

Provides physics movement and other dynamics, and the ability to attach Collider2D to it.
Read time 10 minutesLast updated 5 days ago

Definition

[RequireComponent(typeof(Transform))]public sealed class Rigidbody2D : Component

Remarks

The Rigidbody2D is a fundamental physics component that provides multiple simulation dynamics, such as Rigidbody2D.position and Rigidbody2D.rotation for pose control, and Rigidbody2D.linearVelocity and Rigidbody2D.angularVelocity for velocity control.
You can attach multiple Collider2D to a Rigidbody2D to detect collisions and provide a collision response when you set Rigidbody2D.bodyType to RigidbodyType2D.Dynamic.

Properties

Property

Description

angularDampingThe angular damping of the Rigidbody2D angular velocity.
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.
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.
linearDampingThe linear damping of the Rigidbody2D linear velocity.
linearVelocityThe linear velocity of the Rigidbody2D represents the rate of change over time of the Rigidbody2D position in world-units.
linearVelocityXThe X component of the linear velocity of the Rigidbody2D in world-units per second.
linearVelocityYThe Y component of the linear velocity of the Rigidbody2D in world-units per second.
localToWorldMatrixThe transformation matrix used to transform the Rigidbody2D to world space.
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 Rigidbody2D.mass be automatically calculated from the Collider2D.density of attached colliders?
useFullKinematicContactsShould kinematic/kinematic and kinematic/static collisions be allowed?
worldCenterOfMassGets the center of mass of the rigidBody in global space.

Methods

Method

Description

AddForceApply a force to the rigidbody.
AddForceAtPositionApply a force at a given position in space.
AddForceXAdds a force to the X component of the Rigidbody2D.linearVelocity only leaving the Y component of the world space Rigidbody2D.linearVelocity untouched.
AddForceYAdds a force to the Y component of the Rigidbody2D.linearVelocity only leaving the X component of the world space Rigidbody2D.linearVelocity untouched.
AddRelativeForceAdds a force to the local space Rigidbody2D.linearVelocity. In other words, the force is applied in the rotated coordinate space of the Rigidbody2D.
AddRelativeForceXAdds a force to the X component of the Rigidbody2D.linearVelocity in the local space of the Rigidbody2D only leaving the Y component of the local space Rigidbody2D.linearVelocity untouched.
AddRelativeForceYAdds a force to the Y component of the Rigidbody2D.linearVelocity in the local space of the Rigidbody2D only leaving the X component of the local space Rigidbody2D.linearVelocity untouched.
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.
GetContactCollidersRetrieves all colliders in contact with this Rigidbody, with the results filtered by the
contactFilter
.
GetContactsRetrieves all Colliders in contact with any 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
.
MovePositionAndRotationMoves the rigidbody position to
position
and the rigidbody angle to
angle
.
MoveRotationRotates the Rigidbody to
angle
(given in degrees).
OverlapGet 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".
SlideSlide the Rigidbody2D using the specified
velocity
integrated over
deltaTime
using the configuration specified by
slideMovement
.
WakeUpDisables the "sleeping" state of a rigidbody.

Structs

Struct

Description

Rigidbody2D.SlideMovementThe configuration that controls how a Rigidbody2D.Slide method behaves.
Rigidbody2D.SlideResultsThe results of a slide movement performed with Rigidbody2D.Slide.

Inheritance

Inherited Members

Operators

Operator

Description

operator ==Compares two object references to see if they refer to the same object.
boolDetermines whether the object exists.
operator !=Compares if two objects refer to a different object.