< Return to Video

Colliders - Unity Official Tutorials

  • 0:00 - 0:02
    Colliders are a component that allows
  • 0:02 - 0:04
    the game object they're attached to
  • 0:04 - 0:06
    to react to other colliders
  • 0:06 - 0:08
    provided that one of the game objects
  • 0:08 - 0:10
    has a rigidbody component attached.
  • 0:10 - 0:13
    Colliders come in various shapes and types,
  • 0:13 - 0:15
    and are denoted in the scene view
  • 0:15 - 0:16
    by a green outline.
  • 0:16 - 0:19
    They can have the following primitive shapes:
  • 0:19 - 0:31
    a sphere, a capsule and a box.
  • 0:33 - 0:35
    For more complex shapes you have two options
  • 0:35 - 0:37
    You can either combine several of these
  • 0:37 - 0:40
    primitive shapes together by applying
  • 0:40 - 0:42
    in our hierarchy.
  • 0:42 - 0:43
    primitive colliders to different objects.
  • 0:43 - 0:45
    For example this workbench
  • 0:45 - 0:47
    has a number of objects which simply
  • 0:47 - 0:49
    serve to make up it's different colliders
  • 0:49 - 0:51
    for various areas.
  • 0:51 - 0:54
    The other option is to use a mesh collider,
  • 0:54 - 0:56
    which will fit the exact shape of the
  • 0:56 - 0:58
    mesh that you specify.
  • 0:58 - 1:01
    The workbench on the right has no hierarchy
  • 1:01 - 1:03
    but instead uses a mesh collider.
  • 1:03 - 1:05
    The reason not to use a mesh collider
  • 1:05 - 1:07
    is that it will fit the exact shape
  • 1:07 - 1:09
    of the mesh that you specify. So if you
  • 1:09 - 1:11
    only specify the mesh of your detailed model
  • 1:11 - 1:13
    then it may be providing too detailed
  • 1:13 - 1:16
    a collision mesh and effecting performance.
  • 1:16 - 1:18
    This is the reason why it's often better
  • 1:18 - 1:20
    to make a compound setup instead.
  • 1:20 - 1:22
    However it should be noted that a third
  • 1:22 - 1:24
    option for creating collision geometry
  • 1:24 - 1:27
    is to use a separate, simpler set of
  • 1:27 - 1:30
    geometry and still use a mesh collider.
  • 1:30 - 1:32
    In this example we have this complex
  • 1:32 - 1:34
    robot arm asset.
  • 1:34 - 1:36
    It's a very detailed mesh
  • 1:36 - 1:38
    but we don't want a mesh as complex
  • 1:38 - 1:40
    as this for the collisions.
  • 1:40 - 1:43
    So what we've done is built
  • 1:43 - 1:46
    a secondary set of geometry which we've
  • 1:46 - 1:48
    then applied to a series of mesh colliders.
  • 1:48 - 1:50
    For example this part of the claw is more
  • 1:50 - 1:52
    detailed than the collision mesh
  • 1:52 - 1:53
    that we've created for it.
  • 1:54 - 1:56
    And this has been done in two separate
  • 1:56 - 2:00
    FBX files -The original artwork and
  • 2:00 - 2:02
    a simplified set of geometry.
  • 2:02 - 2:04
    We've then gone through each one,
  • 2:04 - 2:06
    applied a mesh collider and dragged
  • 2:06 - 2:08
    these meshes over as the
  • 2:08 - 2:09
    collision meshes to use.
  • 2:09 - 2:11
    This means that we get the kind of accuracy
  • 2:11 - 2:13
    that we need in terms of collision
  • 2:13 - 2:15
    without the performance overhead.
  • 2:16 - 2:18
    When collisions occur in the game engine
  • 2:18 - 2:20
    one collider strikes another and an event
  • 2:20 - 2:22
    called OnCollisionEnter is called.
  • 2:22 - 2:26
    In this scene our 'prop samoflange' object
  • 2:26 - 2:29
    has a sphere collider component and a rigidbody component.
  • 2:29 - 2:32
    The rigidbody provides mass and gravity.
  • 2:32 - 2:34
    When I play the game, one falls down
  • 2:34 - 2:36
    and strikes the other.
  • 2:37 - 2:40
    The power cube has a box collider attached to it.
  • 2:43 - 2:46
    Also attached to our falling object
  • 2:46 - 2:47
    is this script.
  • 2:48 - 2:50
    This script checks for three collision events.
  • 2:50 - 2:54
    OnCollisionEnter, OnCollisionStay
  • 2:54 - 2:55
    and OnCollisionExit.
  • 2:55 - 2:57
    When each of these occurs it writes
  • 2:57 - 2:59
    to the console using Debug.Log.
  • 3:00 - 3:02
    It will register when Enter is called,
  • 3:02 - 3:04
    when Stay is occurring
  • 3:04 - 3:06
    and when Exit is called.
  • 3:10 - 3:12
    So if we look at our console you can see
  • 3:12 - 3:14
    that Enter is called, Stay has occurred
  • 3:14 - 3:17
    for a while and then Exit is called.
  • 3:17 - 3:20
    If we pause the game and play we can look
  • 3:20 - 3:22
    at this example slowly.
  • 3:22 - 3:24
    As I step through the frames
  • 3:26 - 3:27
    when the collision occurs you can see
  • 3:27 - 3:30
    that Enter is called, so OnCollisionEnter
  • 3:30 - 3:32
    has just occurred.
  • 3:33 - 3:37
    As I continue OnCollisionStay is occurring.
  • 3:37 - 3:39
    You can see on the right here that it's
  • 3:39 - 3:41
    happening several times because these
  • 3:41 - 3:43
    two colliders are still in contact.
  • 3:43 - 3:45
    As we continue to step through
  • 3:45 - 3:48
    eventually OnCollisionExit is called
  • 3:48 - 3:50
    when the two colliders are no longer
  • 3:50 - 3:52
    in contact.
  • 3:53 - 3:55
    Note that for an OnCollision message to
  • 3:55 - 3:57
    to be sent, one of the two objects colliding
  • 3:57 - 4:00
    must have a rigidbody component.
Title:
Colliders - Unity Official Tutorials
Description:

more » « less
Video Language:
English
Duration:
04:02

English subtitles

Revisions