This article will explore Unity’s 2D physical components in detail, and refer to Physics 3D Reference for their 3D equivalents. See Physics 2D for better customizations;

#Rigibody 2D

Programming interface – SWITCH TO SCRIPTING

By adding the Rigibody-2D component, you can put a game object under the control of the physics Engine [1], and many of its concepts are similar to and ported from the standard Rigidbody component. The difference is that rigidbody-2D is used for 2D scenes, and the game object can only move and transform in the XY plane, and can only rotate with the axis perpendicular to the 2D plane.

Depending on the Body Type of the component, the related setting options are different;

  • Working mechanism of Rigidbody 2D

21. Usually, the Unity Editor’s Transform component provides 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 engine is able to move colliders and make them interact with each other, so a method is required for the physics engine 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.

In general, the Transform component defines the position, rotation, and scaling of a GameObject(and its child GameObject) in the scene(called the spatial state), and when its parameters change (or the spatial state of the GameObject changes), It updates other components, such as where the game object is being rendered and where the colliders are located; Correspondingly, the 2D physics engine has the ability to move gameObjects with colliders in the scene and make them collide and interact with each other, which will cause the spatial state of the GameObject to change, so we need a way to feed the new spatial state back to the Transform. This is the implementation of Rigibody 2D for colliders;

The Rigidbody 2D component overrides the Transform and updates it to a position/rotation defined by the Rigidbody 2D. Note that while you can still override the Rigidbody 2D by 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.

The Rigibody 2D component makes the GameObject’s motion subject to the physics engine, rather than manually modifying the Transform. In addition, RigidBody updates the Transform in real time because the GameObject’s spatial state changes. Keep in mind that the object movement controlled by Rigidbody is controlled by the physics engine, while the object movement controlled by Transform is not controlled by the physics engine. When Rigidbody 2D is added to your Gameobject, the object movement is controlled by the physics engine. Don’t use a Transform to control the movement of a GameObject, as this can lead to serious malfunctions such as objects passing through and embedding other objects, and unexpected malfunctions.

Any Collider 2D component added to the same GameObject or child GameObject is implicitly attached to that Rigidbody 2D. When a Collider 2D is attached to the Rigidbody 2D, it moves with it. 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 colliders that act effectively as a single compound collider, all moving and rotating in sync with the Rigidbody 2D.

Any Collider 2D that was added to this GameObject will be attached to Rigidbody 2D implicitly, which means that when you’re using Rigidbody 2D to control the object’s motion, The Collider 2D will move with it. It’s important to remember that if you want a Collider 2D to move properly, you can’t control it using a Transform, Or collider-offset[2], the correct way to use Rigidbody 2D API control, only in this way to ensure the best performance and correct collision detection; Multiple Collider 2D’s attached to the same Rigidbody 2D will not collide with each other. Instead, you can superimpose multiple Collider 2D’s on top of each other to make the collision more pronounced and change space state in sync with the Rigidbody 2D.

When designing a Scene, you are free to use a default Rigidbody 2D and start attaching colliders. These colliders allow any other colliders attached to different Rigidbody 2Ds to collide with each other.

When designing a scene, you are not limited to adding more than one Collider to the default Rigidbody 2D. And interact with Rigidbody 2D, which has a Collider attached to it, in the scene.

[1]physics engine : A system that simulates aspects of physical systems so that objects can accelerate correctly and be affected by collisions, gravity and other forces.More info

[2] Collider-offset: You can think of a Collider as a geometry used for collision detection that is superimposed on and attached to the GameObject Mesh. Rigidbody is not a separate entity from the GameObject. It is added to the GameObject to make it subject to the physics engine. In this case, Rigidbody 2D represents the GameObject;

-Tip

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 physics simplifies many common gameplay mechanics and allows for realistic behavior with minimal coding.

For sprites with Rigidbody 2D, you can apply a force through the Scripting API to make them move in a physically reliable way; Once the appropriate Collider component has been added to the same Sprite, the Sprite will also move in a physically reliable manner if a collision occurs, except that the force applied is not through the API but through the spontaneous reaction of the collision. The physical system provided by Unity3D not only simplifies the mechanical processing of the game by developers, but also realizes realistic movement behavior under the condition of low code amount