Introduction to Collisions and Colliders
In layman’s terms, Collisions are referred to instances in the real world where two objects collide and physically touch each other. However, in Unity 3D, which is a popular gaming engine, collisions are defined using Collider2D. Collisions and colliders are inter-related terms used in the development of games and animations.
Collider2D is a component which defines the region where gameObjects interact with each other during a collision. Therefore, colliders are the regions where gameObjects are solidified and can be interpreted as solids interacting and colliding with each other. Regions are considered instead of definitive shapes because not all gameObjects are shaped exactly as they are defined. For example, in the case of definite shapes like squares, triangles, etc., we can specifically define boundaries covering exact edges and manipulate the region area around them. This can be visualized in the following figures of triangles and squares.
Source: Link
However, in the case of shapes with indefinite boundaries like the gear shape, we consider approximate shapes and therefore, an approximated regional area is taken into account for these gameObjects. The green boundaries define the collision region for each shape in the figure. For normal shapes, the green boundaries are definite and well-defined but note how the collider doesn’t wrap around the gear’s teeth perfectly, but instead, Unity made approximations on the collision regions of the shape, leading to a simpler collision region.
Collider2D is a defined component in Unity that detects collision or intersection of gameObjects in a defined 2-D coordinate space and tells when the objects are touching or even overlapping.
If the gameObject sprite is a complex drawing, just an approximation of its shape will be necessary to work because the Collider 2D is invisible and not distinguishable during the gameplay.
Trigger 2D is a particular behaviour of a Collider2D which is used when we want to detect when one collider overlaps another one without creating a collision.
RigidBody 2D is a component allows the physics engine to control the object.
How to add a Collider Component in Unity?
To add a collider2D component in Unity, we use the Add Component option present in the Physics 2D section of the Box Collider. Select the Gameobject in the scene and click on add component.
Source: Link
Type “collider 2D” or “rigidbody 2D” in the search box and select the component.
Source: Link
After selecting the component we can see the collider in this way for a polygon:
Source: Link