Rigidbody and Collider2D components
2D Game Design with Unity
🕑 This lesson will take about 12 minutes
In this lesson, you will learn how to add a Rigidbody and Colliders to game objects in a 2D game built in Unity.
A Rigidbody allows objects in your game to act under the control of physics. This includes reacting to forces and gravity, mass, drag and momentum. A Rigidbody component can be added to a GameObject by selecting the GameObject in your scene and clicking ‘Add Component’ from the Inspector panel.
Rigidbodies are essential for objects in your game that will need to move around, fall, bounce, roll, or physically interact with other GameObjects in your scenes.
To implement player movement, you would usually need to add both a Rigidbody and Collider2D component to the player GameObject.
For the player to interact with other objects (eg. walk on a platform or ground), those objects would at least need a Collider2D component if they are static (don’t move or physically react) and both a Collider2D component and Rigidbody component if they are to physically react.
A Collider2D allows us to define the physical shape of an object so that it can collide with other objects. It is often referred to as a “hit box”. A Collider2D is invisible in the game. There can be different types of collisions specified - such as
two objects hitting/colliding into each other,
an object entering another’s collider,
an object staying inside another object’s collider, or
an object leaving another object’s collider.
Colliders are essential for interaction in your game. If you want a player to be able to walk on platforms and not fall through them, then both the player object and platform objects will need a Collider2D. If you want to be able to pick up items or detect when the player has entered a specific area (eg. reached a checkpoint or fallen off a platform) then colliders can be used.
In Unity, there are different Collider2D shapes you can use depending on the shape of your object:BoxCollider2D
CapsuleCollider2D
CircleCollider2D
CompositeCollider2D
CustomCollider2D
EdgeCollider2D
PolygonCollider2D
TilemapCollider2D
We will look at some of these Collider2D shapes in the video and later on in the course.
Watch the video below to learn how to add and use Rigidbody and Collider2D components to your game.
Game art and sprites from the Free Pixel Space Platform pack are used in this lesson video and can be downloaded from the Unity Asset Store.
Is YouTube blocked at school? Watch on Google Drive.
Next lesson: Physics Materials 2D