To have any physical presence in the scene
a game object must use one of the
collider components to define it's physical shape
and to interact with the relevant physics engine.
Unity has 2 physics engines.
One for 3D and one for 2D.
These two physics engines do not interact with each other.
For more information on physics and physics 2D
please see the information linked below.
When using the 2D physics engine
a collider 2D component must be used.
There are 4 collider 2D components to choose from.
The circle collider 2D,
the box collider 2D,
the polygon collider 2D
and the edge collider 2D.
All of these collider components share many properties
as they all derive from the same generic
base collider 2D.
Each collider 2D component however is
optimised for a particular shape.
The circle collider 2D works with
round circular objects.
The box collider 2D works well with rectangular objects.
The polygon collider 2D work
best with complex shapes.
And the edge collider 2D works well for surfaces.
The edge collider and polygon colliders are very similar.
They are created by several points, or vertices,
with edges connecting them.
The main difference between them is that the polygon collider
must be a closed shape covering an area.
And the edge collider must be open,
defining an edge of one or more segments.
One good use of the edge collider
is to make a single solid surface
rather than a series of 2D colliders
even if several sprites are used to create
the visual aspect of the surface.
When comparing all 2D colliders
there are 2 properties exposed in the inspector
that they all share.
Is Trigger and Material.
The other unique properties are for defining
the shape of that particular 2D collider.
The Is Trigger property will set the collider
to behave as a trigger collider.
With normal 2D physics collisions
the 2D physics engine will prevent
2D colliders from passing through each other.
2D trigger colliders will not participate
in physical collisions and will send 2D
trigger messages rather than 2D collision messages.
These trigger messages can be used to
initiate new actions in the scene.
For more information on Is Trigger
and the messages sent by colliders
please see the information linked below.
The material property is a reference to the
physics material 2D being used by the collider 2D
and this can be none.
Physics materials define how colliders
will react when they collider
and define both the bounciness and
the friction of the collider's surface.
To make a bouncy ball or slippery ice covered platform
use a different physics material 2D
with each collider 2D.
For more information on physics material 2D
please see the information linked below.
To use a collider 2D it must be attached
to a game object.
One way of attaching a collider 2D component
is to use the Add Component button
and select Physics 2D
and select a Collider 2D Component.
For more information on game objects,
components and how to use them
please see the information linked below.
When a collider 2D component is attached
Unity will try to size the collider 2D to the sprite.
If the size and shape of the collider 2D
is undesirable then the shape can be edited by either
changing the values in the inspector
or changing the collider 2D itself in the scene view.
To edit a collider 2D in the scene view
hold down the shift key.
This will display handles on the 2D collider's
gizmo in the scene view.
These handles are drag-able.
In the case of the edge collider and the polygon collider
dragging an existing handle will move
the collider's vertex, and change the collider's shape
rather than resizing the collider.
By moving the cursor over an empty edge
on either the edge collider or the polygon collider
a new vertex can be created by clicking.
This is indicated by a new handle appearing
and the edge highlighting in green.
In the case of the edge collider and the polygon collider
holding the control key will allow a
vertex to the be deleted when clicked.
This is indicated by the handle
and the edge highlighting in red.
A game object does not need a rigidbodied
2D to use a collider 2D.
To participate in 2D collision and trigger events
however at least 1 of the game objects
involved must have a rigidbody 2D attached.
A common pattern for best performance
would be to attach collider 2D components
but not rigidbody components
to all of the static, or non-moving game objects
that need to interact with 2D physics.
And attach a collider 2D and a rigidbody 2D
to the dynamic, or moving, game object
that need to interact in the scene.
For more information on physics 2D
collider 2D, collisions and triggers
please see the information linked below.
Subtitles by the Amara.org community