0
0
Unityframework~7 mins

Rigidbody2D component in Unity

Choose your learning style9 modes available
Introduction

The Rigidbody2D component lets objects in a 2D game move and react to forces like gravity and collisions, making them behave like real things.

When you want a 2D character to fall naturally due to gravity.
When you want a ball to bounce off walls in a 2D game.
When you want to push or pull objects with forces or impulses.
When you want to detect collisions and respond to them physically.
When you want smooth and realistic movement controlled by physics.
Syntax
Unity
Rigidbody2D rb = gameObject.AddComponent<Rigidbody2D>();
rb.gravityScale = 1.0f;
rb.mass = 1.0f;
rb.velocity = new Vector2(2f, 0f);

You add Rigidbody2D to a GameObject to make it affected by 2D physics.

Properties like gravityScale and mass control how it moves and reacts.

Examples
This makes the object not fall and have double the normal mass.
Unity
Rigidbody2D rb = gameObject.AddComponent<Rigidbody2D>();
rb.gravityScale = 0; // No gravity
rb.mass = 2.0f;
This sets the object's speed to move right at 5 units per second.
Unity
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(5f, 0f);
This gives the object a quick push upward like a jump.
Unity
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.AddForce(new Vector2(0f, 10f), ForceMode2D.Impulse);
Sample Program

This script adds a Rigidbody2D to the object, so it falls with gravity. When you press space, it pushes the object up like a jump and prints a message.

Unity
using UnityEngine;

public class SimpleMover : MonoBehaviour
{
    private Rigidbody2D rb;

    void Start()
    {
        rb = gameObject.AddComponent<Rigidbody2D>();
        rb.gravityScale = 1.0f;
        rb.mass = 1.0f;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rb.AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
            Debug.Log("Jump force applied!");
        }
    }
}
OutputSuccess
Important Notes

Remember to add a Collider2D component to detect collisions properly with Rigidbody2D.

Setting gravityScale to 0 makes the object ignore gravity but still respond to forces.

Use ForceMode2D.Impulse for instant pushes, and ForceMode2D.Force for continuous forces.

Summary

Rigidbody2D makes 2D objects move and react like real things using physics.

You control movement by changing properties or applying forces.

Combine Rigidbody2D with Collider2D for collision detection and response.