< Return to Video

Collider 2D - Official Unity Tutorial

  • 0:01 - 0:04
    To have any physical presence in the scene
  • 0:04 - 0:06
    a game object must use one of the
  • 0:06 - 0:09
    collider components to define it's physical shape
  • 0:09 - 0:12
    and to interact with the relevant physics engine.
  • 0:13 - 0:15
    Unity has 2 physics engines.
  • 0:15 - 0:18
    One for 3D and one for 2D.
  • 0:18 - 0:22
    These two physics engines do not interact with each other.
  • 0:23 - 0:26
    For more information on physics and physics 2D
  • 0:26 - 0:28
    please see the information linked below.
  • 0:29 - 0:31
    When using the 2D physics engine
  • 0:31 - 0:35
    a collider 2D component must be used.
  • 0:36 - 0:39
    There are 4 collider 2D components to choose from.
  • 0:40 - 0:42
    The circle collider 2D,
  • 0:42 - 0:44
    the box collider 2D,
  • 0:44 - 0:46
    the polygon collider 2D
  • 0:46 - 0:49
    and the edge collider 2D.
  • 0:50 - 0:53
    All of these collider components share many properties
  • 0:53 - 0:55
    as they all derive from the same generic
  • 0:55 - 0:57
    base collider 2D.
  • 0:58 - 1:00
    Each collider 2D component however is
  • 1:00 - 1:02
    optimised for a particular shape.
  • 1:03 - 1:05
    The circle collider 2D works with
  • 1:05 - 1:07
    round circular objects.
  • 1:08 - 1:12
    The box collider 2D works well with rectangular objects.
  • 1:12 - 1:14
    The polygon collider 2D work
  • 1:14 - 1:16
    best with complex shapes.
  • 1:17 - 1:21
    And the edge collider 2D works well for surfaces.
  • 1:23 - 1:26
    The edge collider and polygon colliders are very similar.
  • 1:27 - 1:30
    They are created by several points, or vertices,
  • 1:30 - 1:32
    with edges connecting them.
  • 1:33 - 1:36
    The main difference between them is that the polygon collider
  • 1:36 - 1:39
    must be a closed shape covering an area.
  • 1:40 - 1:42
    And the edge collider must be open,
  • 1:42 - 1:45
    defining an edge of one or more segments.
  • 1:46 - 1:48
    One good use of the edge collider
  • 1:48 - 1:50
    is to make a single solid surface
  • 1:50 - 1:53
    rather than a series of 2D colliders
  • 1:53 - 1:55
    even if several sprites are used to create
  • 1:55 - 1:58
    the visual aspect of the surface.
  • 2:00 - 2:02
    When comparing all 2D colliders
  • 2:02 - 2:04
    there are 2 properties exposed in the inspector
  • 2:04 - 2:06
    that they all share.
  • 2:06 - 2:09
    Is Trigger and Material.
  • 2:10 - 2:12
    The other unique properties are for defining
  • 2:12 - 2:15
    the shape of that particular 2D collider.
  • 2:16 - 2:18
    The Is Trigger property will set the collider
  • 2:18 - 2:20
    to behave as a trigger collider.
  • 2:21 - 2:23
    With normal 2D physics collisions
  • 2:23 - 2:25
    the 2D physics engine will prevent
  • 2:25 - 2:28
    2D colliders from passing through each other.
  • 2:30 - 2:32
    2D trigger colliders will not participate
  • 2:32 - 2:35
    in physical collisions and will send 2D
  • 2:35 - 2:39
    trigger messages rather than 2D collision messages.
  • 2:39 - 2:41
    These trigger messages can be used to
  • 2:41 - 2:44
    initiate new actions in the scene.
  • 2:45 - 2:47
    For more information on Is Trigger
  • 2:47 - 2:49
    and the messages sent by colliders
  • 2:49 - 2:51
    please see the information linked below.
  • 2:53 - 2:56
    The material property is a reference to the
  • 2:56 - 3:00
    physics material 2D being used by the collider 2D
  • 3:00 - 3:02
    and this can be none.
  • 3:02 - 3:04
    Physics materials define how colliders
  • 3:04 - 3:06
    will react when they collider
  • 3:06 - 3:08
    and define both the bounciness and
  • 3:08 - 3:10
    the friction of the collider's surface.
  • 3:11 - 3:15
    To make a bouncy ball or slippery ice covered platform
  • 3:15 - 3:17
    use a different physics material 2D
  • 3:17 - 3:19
    with each collider 2D.
  • 3:20 - 3:23
    For more information on physics material 2D
  • 3:23 - 3:26
    please see the information linked below.
  • 3:27 - 3:30
    To use a collider 2D it must be attached
  • 3:30 - 3:32
    to a game object.
  • 3:32 - 3:35
    One way of attaching a collider 2D component
  • 3:35 - 3:37
    is to use the Add Component button
  • 3:37 - 3:39
    and select Physics 2D
  • 3:39 - 3:42
    and select a Collider 2D Component.
  • 3:43 - 3:45
    For more information on game objects,
  • 3:45 - 3:47
    components and how to use them
  • 3:47 - 3:50
    please see the information linked below.
  • 3:51 - 3:53
    When a collider 2D component is attached
  • 3:53 - 3:57
    Unity will try to size the collider 2D to the sprite.
  • 3:57 - 3:59
    If the size and shape of the collider 2D
  • 3:59 - 4:03
    is undesirable then the shape can be edited by either
  • 4:03 - 4:05
    changing the values in the inspector
  • 4:07 - 4:11
    or changing the collider 2D itself in the scene view.
  • 4:11 - 4:14
    To edit a collider 2D in the scene view
  • 4:14 - 4:16
    hold down the shift key.
  • 4:17 - 4:19
    This will display handles on the 2D collider's
  • 4:19 - 4:21
    gizmo in the scene view.
  • 4:22 - 4:24
    These handles are drag-able.
  • 4:26 - 4:29
    In the case of the edge collider and the polygon collider
  • 4:29 - 4:31
    dragging an existing handle will move
  • 4:31 - 4:34
    the collider's vertex, and change the collider's shape
  • 4:34 - 4:37
    rather than resizing the collider.
  • 4:38 - 4:40
    By moving the cursor over an empty edge
  • 4:40 - 4:43
    on either the edge collider or the polygon collider
  • 4:43 - 4:46
    a new vertex can be created by clicking.
  • 4:47 - 4:49
    This is indicated by a new handle appearing
  • 4:49 - 4:52
    and the edge highlighting in green.
  • 4:58 - 5:01
    In the case of the edge collider and the polygon collider
  • 5:01 - 5:03
    holding the control key will allow a
  • 5:03 - 5:05
    vertex to the be deleted when clicked.
  • 5:06 - 5:08
    This is indicated by the handle
  • 5:08 - 5:10
    and the edge highlighting in red.
  • 5:16 - 5:19
    A game object does not need a rigidbodied
  • 5:19 - 5:21
    2D to use a collider 2D.
  • 5:22 - 5:25
    To participate in 2D collision and trigger events
  • 5:25 - 5:27
    however at least 1 of the game objects
  • 5:27 - 5:30
    involved must have a rigidbody 2D attached.
  • 5:31 - 5:33
    A common pattern for best performance
  • 5:33 - 5:36
    would be to attach collider 2D components
  • 5:36 - 5:38
    but not rigidbody components
  • 5:38 - 5:40
    to all of the static, or non-moving game objects
  • 5:40 - 5:43
    that need to interact with 2D physics.
  • 5:43 - 5:46
    And attach a collider 2D and a rigidbody 2D
  • 5:46 - 5:49
    to the dynamic, or moving, game object
  • 5:49 - 5:51
    that need to interact in the scene.
  • 5:54 - 5:56
    For more information on physics 2D
  • 5:56 - 5:58
    collider 2D, collisions and triggers
  • 5:58 - 6:01
    please see the information linked below.
Title:
Collider 2D - Official Unity Tutorial
Description:

more » « less
Video Language:
English
Duration:
06:09

English subtitles

Revisions