Attaching a Rigidbody 2D component to a GameObject allows it to be controlled by the physics system. The Rigidbody 2D shares many similar properties with its standard Rigidbody counterpart but adapted to 2D development. For example, GameObjects can only move along the XY plane and can only rotate on an axis perpendicular to that plane.
The Unity Editor’s Transform component defines how a GameObject (and its child GameObjects) is positioned, rotated and scaled within the Scene. When it is changed, it updates other components which may update things like where they render or where Colliders are positioned. The 2D physics system is able to move Colliders and make them interact with each other, so a method is required for the physics system to communicate this movement of Colliders back to the Transform components. This movement and connection with Colliders is what a Rigidbody 2D component is for.
The Rigidbody 2D component overrides the Transform component and updates it to a position/rotation defined by the Rigidbody 2D. Note: While you can still override the Rigidbody 2D by directly modifying the Transform component yourself (because Unity exposes all properties on all components), doing so will cause problems such as GameObjects passing through or into each other, and unpredictable movement.
Any Collider 2D component added to the same GameObject or child GameObject is implicitly attached to that Rigidbody 2D, causing the Collider 2D to move with the Rigidbody 2D. When attached, a Collider 2D should never be moved directly using the Transform or any Collider offset; the Rigidbody 2D should be moved instead. This offers the best performance and ensures correct collision detection. Collider 2Ds attached to the same Rigidbody 2D won’t collide with each other. This means you can create a set of Colliders that act effectively as a single compound Collider, all moving and rotating in sync with the Rigidbody 2D.
Adding a Rigidbody 2D allows a Sprite to move in a physically convincing way by applying forces from the scripting API. When the appropriate Collider component is also attached to the Sprite GameObject, it is affected by collisions with other moving GameObjects. Using the Unity physics system potentially simplifies many common gameplay mechanics and allows for realistic behavior with minimal coding.
Note: Although Rigidbody 2Ds are often described as colliding with each other, it is the Collider 2Ds attached to each of those bodies which collide. Rigidbody 2Ds cannot collide with each other without Colliders.
The Rigidbody 2D component’s available properties in its Inspector window are different depending which Body Type you have selected. Refer to the respective Body Type pages for detailed information about their property settings.
There are three options for Body Type which define the behavior of the Rigidbody 2D. Any Collider 2D attached to that Rigidbody 2D inherits the Rigidbody 2D’s Body Type as well. The three available options are:
The selected Body Type defines the Rigidbody 2D’s movement behavior (position and rotation) and Collider interactions. When a Body Type changes, various mass-related internal properties are recalculated immediately, and all existing contacts for the Collider 2Ds attached to the Rigidbody 2D need to be re-evaluated during the GameObject’s next FixedUpdate. Depending on how many contacts and Collider 2Ds are attached to the body, changing the Body Type can cause variations in performance.
The Simulated property is common to all available Body Types. Use this property to start (enabled) or stop (disabled) a Rigidbody 2D and any attached Collider 2Ds and Joint 2Ds from interacting with the 2D physics simulation. Changing this property is much more memory and processor efficient than enabling or disabling individual Collider 2D and Joint 2D components.
When Simulated is enabled, the following occurs:
When Simulated is disabled, the following occurs:
You can stop and start individual elements of the 2D physics simulation by enabling and disabling physics related components individually on both Collider 2D and Joint 2D components. However, enabling and disabling individual elements of the physics simulations means internal GameObjects and physics-based components are constantly created and destroyed, potentially costing high memory usage and processor power. It is thus more efficient to disable the physics simulation entirely rather than disabling the individual components.
Note: When a Rigidbody 2D’s Simulated option is disabled, any attached Collider 2D is effectively ‘invisible’ and cannot be detected by any physics queries, such as with Physics.Raycast.