Rigidbodies and Colliders
メニューを表示するにはスワイプしてください
Physics: Rigidbodies and Colliders
The game now has a living character, but it isn't interacting with the environment yet. That's because we haven't added any physics: things like gravity and collisions.
The good news is that Unity already has a built-in physics system, so we don't need to program any of this from scratch.
Adding Physics with a Rigidbody2D
To give the player physics, add a Rigidbody2D component.
Select the player GameObject, click Add Component, and search for Rigidbody2D.
Because this is a 2D game, it's important to choose Rigidbody2D, not Rigidbody.
Now when you press Play, the player is affected by gravity and starts to fall (or sink!).
Why the Player Falls Through the Ground
At this point, the player falls straight through the ground and platforms. This happens because Unity handles collisions using colliders, and we haven't added any yet.
A collider defines the physical shape of a GameObject - where it can collide with other objects.
Adding Colliders
Any GameObject that should collide needs a Collider 2D component.
Start with the player:
- Select the player GameObject;
- Search for Collider 2D;
- Choose a Box Collider 2D for simplicity.
In the Scene view, you'll see a green outline around the player. This represents the collision area. You can adjust it by editing the collider and dragging its edges.
Make sure the bottom of the collider lines up with the character's feet, so collisions feel natural.
Next, add colliders to the environment:
- For platforms, choose a collider shape that fits the sprite (if unsure, use Box Collider 2D);
- If you have a floor object, add a Box Collider 2D and adjust its dimensions.
Now when you press Play, the player should fall and land on platforms instead of passing through them.
Preventing the Player from Toppling Over
You may notice the player rotating when it collides with the edges of platforms.
For most 2D platformers, this isn't desirable.
To fix this:
- Select the player;
- In the Rigidbody2D, freeze rotation on the Z axis.
This prevents the sprite from tipping over due to collisions.
Customizing Gravity and Weight
With basic physics working, we can now tweak how the player feels.
Increasing the Gravity Scale makes the player fall faster, which can be great for fast-paced games. However, high gravity also means the player can reach very high speeds very quickly.
To balance this, you can increase Linear Drag:
- High gravity + high drag makes the player start falling immediately;
- But limits how fast they can fall.
This gives you more control over the game's pacing.
Improving Collision Reliability and Smoothness
Two important Rigidbody2D settings are worth enabling on the player:
- Collision Detection → Continuous: this makes collisions more reliable and helps prevent the player from glitching through platforms.
- Interpolation → Interpolate: this smooths the player's movement and reduces small visual stutters.
With a Rigidbody2D and colliders in place, your player now has weight, falls due to gravity, and interacts properly with the world. This is the foundation for jumping, movement, and all the gameplay mechanics we'll build next.
1. Why does the player fall through the ground at first?
2. What is the purpose of freezing the Z rotation on a Rigidbody2D?
3. What does setting Collision Detection to Continuous do?
フィードバックありがとうございます!
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください