In this lesson, you will learn how to manipulate the Rigidbody component to govern how your game objects move and respond to forces within Unity’s physics engine.
The Physics Trifecta: Mass, Drag, and Gravity
When you add a Rigidbody to a GameObject, you are essentially telling the engine to take control of that object's movement. By adjusting three specific settings in the Inspector, you can define how that object "feels" in your game world.
- Mass: This defines how much "heft" an object has. A higher mass makes an object harder to push and gives it more momentum when colliding with other objects. If you have a wrecking ball and a beach ball, the wrecking ball should have a significantly higher mass to reflect its impact force.
- Drag: This is essentially "air resistance." If you set Drag to
0, an object will fly through the air indefinitely once a force is applied. If you increase the Drag, the object will experience a force opposing its motion, causing it to slow down over time as if it were moving through water or thick syrup. - Use Gravity: This is a simple toggle. When checked, the global physics gravity (defaulting to -9.81 on the Y-axis) acts upon the object. Unchecking this is perfect for space-themed games or floating power-ups.
Controlling Motion via Script
While you can set these values in the Inspector, you can also manipulate them through code to create dynamic gameplay elements, such as a character becoming "heavier" when picking up a shield:
void ApplyPhysicsChanges(Rigidbody rb)
{
// Make the object heavier
rb.mass = 10f;
// Increase air resistance to make it feel sluggish
rb.drag = 2.5f;
// Disable gravity for a floaty effect
rb.useGravity = false;
}
By balancing these three properties, you move away from static, robotic movement and into a world where objects feel like they truly inhabit a physical space. Always test these values in the FixedUpdate cycle if you are applying manual forces, as that is where the physics engine performs its calculations.
Key Takeaway: Tuning mass, drag, and gravity allows you to define the unique physical "personality" of every dynamic object in your game.