Unity 2021: Rigidbody2D the component for physics

In this blog we will explain the properties of the Unity Rigidbody2D component, with the aim that you achieve a more efficient use of this component and thus become a better video game developer.

Content

1.What is the Rigidbody2D component?
2.How does the Unity Rigidbody2D component work and how to add it to an object?
3.Properties of the Rigidbody2D component
4.Other properties of the Rigidbody2D
5.Other performance recommendations

1.What is the Rigidbody2D component?

The Rigidbody2D is the component in charge of equipping your game object with the ability to perceive and respond to physical forces, allowing it to be affected by gravity and forces of movement or rotation.

2.How does the Unity Rigidbody2D component work and how to add it to an object?

The Rigidbody2D works as a bridge between the external forces that are applied to the object and the colliders that in turn transmit it to the transform component.

In such a way that the Rigidbody modifies the transform component, that is why you must be clear about how you will move your object that can be done from the Rigidbody component or the transform component and as a recommendation never do it from both components at the same time to avoid erratic movement and improve overall performance.

Also for performance reasons whenever you need to move an object it is useful to add a Rigidbody2D and not just move it from the transform. If you do not want it to be affected by the forces of gravity, you just have to select the BodyTipe “Kinematic” that we will be explained later.

It is also useful to remember that the Rigidbody component requires a collider components to detect collisions. If your object doesn’t have colliders it will never collide even if it has a rigidbody.

To use this component go to the Hierarchy> CreateEmpty with this we will create an empty game object, if you want call it “Object” if you select it in the inspector you will find the transform component which has the properties, “Position”, “Rotation” and “Scale”.

Now where it says “Add component” look for the component “Rigidbody2D” and select it.

The following component will be added.

In the next section we will explain each property

3.“Body Type” property of the Rigidbody2D component

The Rigidbody2D component has different properties that allow you to manipulate how gravity and other forces interact with the object, but the most important property is the “Body Type”, it defines how the Rigidbody2D will behave with respect to the forces that will be applied to the object and the interaction with colliders, there are three options “Dynamic”, “Kinematic” and “Static”.

  • BodyType> Dynamic: It is the one that generates the most processing cost since it is the one that allows your game object to interact with all types of physical forces.
  • BodyType> Kinematic: It is used to save system resources, since the object will only move via script, forces such as gravity will not affect it. Usually the “Kinematic” will not collide with other objects with Rigidbody “Kinematic” or “Static”, only with the Rigidbody Body Type “Dynamic”. The “Kinematic” will be immovable unless we move them by script. They are useful for platforms with fixed movements.
  • BodyType> Static: It is the option that uses the least resources, it is made to not move so if you move it via script, resources will be consumed in the wrong way. This Body Type only collides with the Rigidbody “Dynamic”. An important fact is that if we have an object that has a collider component without Rigidbody, this object will behave as if it had a “Static” type Rigidbody, so when moving it by script it will consume more resources.

4.Other properties of the Rigidbody2D.

The following properties depend on the selected Body Type:

  • Material: In this space we can drag a “Physic Material” which will give your object certain properties. In another blog we explain how to set a physics material and its properties.
  • Simulated: It will allow the Rigidbody to interact with external forces as gravity by default is active, this property is created to facilitate deactivating or activating the physics simulation in a more efficient way, being much more practical than deactivating the entire component.
  • Auto mass: Causes the Rigidbody to automatically detect mass based on the size of your object’s collider.
  • Mass: The mass of the object is measured in kg. Two objects that collide with different masses will respond differently.
  • Drag: It will define how much air resistance the object generates. If it is 0 there will be no resistance, the higher the number the faster the object will slow down. If two objects have the same mass this option will determine which of the two will fall faster in the simulation. A value of 0 will simulate an object with low resistance and a value of 10 a high resistance object such as a feather.
  • Angular Drag: How much air resistance the object generates when it is rotating or under the effect of rotating forces
  • Gravity scale: It will determine to what degree the object will be affected by gravity. If it is 0 the game object will not be affected, the higher the value, the more repercussions the gravity will have on the object. Remember that the Body Type “Kinematic” will not be affected by external forces
  • Collision detection: Used to prevent objects from passing to another collider if they go very fast, this occurs since if our object goes very fast it may happen that in the current frame it is approaching an object but in the next frame it already crosses the object so if we do not adjust this option we will have future problems with fast movements.
    • Discrete: set by default, if your objects don’t go very fast there should be no problem with leaving it like this
    • Continuous: It will prevent your object from passing through others but will have a performance cost. What Unity will do is calculate the contact point between two colliders before it happens in the next frame.
  • Sleeping mode: it will determine when the physics simulations will be activated for your object, adjusting it can improve the performance of your game.
    • Never sleep: it consumes more resources, since our object will always be inside the physics simulation.
    • Start awake: the object will be activated when starting the scene
    • Start asleep: the object will activate the physics simulation when detecting collisions
  • Interpolate: Used to improve the desynchronization between, for example, the Physics Update and the Graphics Update, in other words it serves above all for the desynchronization between your game object and the stage. So Unity recommends only activating it for your main character. By default it is disabled.
    • None: There is no smoothing
    • Interpolate: Anti-aliasing is generated using previous frames
    • Extrapolate: The smoothing is generated with the estimation of future frames
  • Constraints: Constrain the movement of your object for the x, y and z axes
    • Freeze position: restricts movement in x and y
    • Freeze rotation: restricts movement on the z axis

For the “Body Type”> “Kinematic” there is an extra property:

  • Use Full Kinematic Contacts: As we mentioned before the Rigidbody Body Type “Kinematic” only collide with the “Dynamic” if we want our “Kinematic” object to collide with all the options of “Body Type” we must select this option, it will make the rigidbody2d “Kinematic” collide also with other “Kinematic”, if it is not selected it will only collide with the “Dynamic”.

5.Other performance recommendations

  • Remember that everything related to physics must be updated in the FixedUpdate () and not in the Update ()
  • Modify the collision matrix: It escapes the intention of this blog but here you can see how to adjust the collision matrix option that can improve the performance of your game.
  • Determine if you require a 2D or 3D Rigidbody before adding the component to your object.
  • Modify the “Fixed Timestep”: Modify the time between updates of the FixedUpdate ()

With that we end our Rigidbody2D blog, we hope it has been useful to you.

As always thank you so much for reading our blogs!!
If you find this blog useful let us a comment, and follow us in our Instagram @tortugamedev and Twitter @Totuas2.
And don’t forget to check out the rest of our blogs about game development and our games!!

BIBLIOGRAFÍA

Leave a Comment

Your email address will not be published.