Legacy Documentation: Version 2017.2 (Go to current version)
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

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 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.

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.
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.
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.
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.
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.
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.
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.
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.
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 Methods

DestroyRemoves a gameobject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
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.
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.
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答