Legacy Documentation: Version 5.4
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Rigidbody2D

class in UnityEngine

/

Inherits from: Component

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

Sumbission failed

For some reason your suggested change could not be submitted. Please try again 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 in 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.

Variables

angularDragCoefficient of angular drag.
angularVelocityAngular velocity in degrees per second.
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.
freezeRotationControls whether physics will change the rotation of the object.
gravityScaleThe degree to which this object is affected by gravity.
inertiaThe rigidBody rotational inertia.
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.
simulatedIndicates whether the rigid body should be simulated or not by the physics system.
sleepModeThe sleep state that the rigidbody will initially be in.
useAutoMassShould the total rigid-body mass be automatically calculated from the [[Collider2D.density]] of attached colliders?
velocityLinear velocity of the rigidbody.
worldCenterOfMassGets the center of mass of the rigidBody in global space.

Public Functions

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.
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.
GetVectorGet a local space vector given the vector vector in rigidBody global space.
IsAwakeIs the rigidbody "awake"?
IsSleepingIs the rigidbody "sleeping"?
IsTouchingCheck whether any of the collider(s) attached to this rigidbody are touching the collider 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).
OverlapPointCheck if any of the Rigidbody2D colliders overlap a point in space.
SleepMake the rigidbody "sleep".
WakeUpDisables the "sleeping" state of a rigidbody.

Inherited members

Variables

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 (null if there is none attached).
hideFlagsShould the object be hidden, saved with the scene or modifiable by the user?
nameThe name of the object.

Public Functions

BroadcastMessageCalls the method named methodName on every MonoBehaviour in this game object or any of its children.
CompareTagIs this game object tagged with tag ?
GetComponentReturns the component of Type type if the game object has one attached, null if it doesn't.
GetComponentInChildrenReturns the component of Type type in the GameObject or any of its children using depth first search.
GetComponentInParentReturns the component of Type type in the GameObject or any of its parents.
GetComponentsReturns all components of Type type in the GameObject.
GetComponentsInChildrenReturns all components of Type type in the GameObject or any of its children.
GetComponentsInParentReturns all components of Type type in the GameObject or any of its parents.
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.
GetInstanceIDReturns the instance id of the object.
ToStringReturns the name of the game object.

Static Functions

DestroyRemoves a gameobject, component or asset.
DestroyImmediateDestroys the object obj immediately.
DontDestroyOnLoadMakes the object target not be destroyed automatically when loading a new scene.
FindObjectOfTypeReturns the first active loaded object of Type type.
FindObjectsOfTypeReturns a list of all active loaded objects of Type type.
InstantiateReturns a copy of the object original.

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.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961