Understanding Unity 3D Rigidbodies
A rigidbody is a property, which, when added to any object, allows it to interact with a lot of fundamental physics behaviour, like forces and acceleration. You use rigidbodies on anything that you want to have mass in your game.
In other words, adding a Rigidbody to a gameObject is the way of telling Unity,
Okay, see this object here? I want it to behave like it has some mass and weight, and that it should react to forces and collisions in a realistic manner.
When we work with 2D game design, we use Rigidbody2D, not Rigidbody. They're different in the sense that Rigidbody2D interacts only in the
XY axis and overall, works in 2 dimensions. The Rigidbody 2D component overrides the Transform and updates it to a position/rotation defined by the Rigidbody 2D. This helps in adding velocities and accelerations to the object when needed.
To further understand Rigidbodies, here's an example. Consider you have a normal cardboard box, shaped like a cube. Consider that the box itself doesn't have a defined weight for now, it's just a box that exists. If you wanted to move around the box, you could do it in a variety of different ways. Imagine that you want the cardboard box to behave like it's an actual, real world object with some mass. Let's say we filled it with sand. Then, we have a cardboard box that has actual mass, and feels heavy. If you pull or push it, you can expect to have to apply some effort before it moves. Rigidbodies act like the sand in this example. They add a sense of mass to the gameObject (that is, your cardboard box) so that it can interact with forces and other physics fundamentals like gravity and friction.
Enough about describing what a Rigidbody is, let's dig into the game. In the inspector for the gameObject, like Mr. Star, click on Add Component on the bottom. If you didn't modify the gameObject, the button should be just below the Sprite Renderer component.
From there, go to Physics 2D → RigidBody2D. This will add a Rigidbody2D component to your gameObject.
And a new row of RigidBody 2D features is added in the interaction view, just below the Sprite Renderer.
Let's run our game and see what happens. "Uh...What?" is probably what comes out of your mouth at this point. You'll notice that your gameObject drops like a rock! That's not really fun, is it?
To fix that, simply set the Gravity Scale value of the Rigidbody's properties to
Although there's nothing really different, is there? You had a sprite in the game before, and adding a Rigidbody and setting its gravity to zero seems to have brought us back to the exact same point.
Well, not really. You see, by changing the gravity scale of that Rigidbody, you're basically defining how much the object is affected by gravity. In fact, try changing around the mass and gravity scale to different values and see what happens. The object still has a mass, it just doesn't care for the forces of gravity acting on it, and thus remains where it is.
Feel free to explore more options of Rigidbody by changing values of other attributes ans seeing its affects on the gameObiect.