Chapter 2. Using Different Colliders for Interaction

As mentioned in the previous chapter, colliders are among one of the main components of Unity3D. Using colliders, we define a shape for the object that helps in collision detection. In this chapter, we will learn about colliders. We will learn about the following topics:

  • Primitive colliders and their implementation
  • Nonprimitive colliders and their implementations
  • Uses of Trigger Colliders
  • Editing Polygon Collider 2D

Let's have a look at the primitive and nonprimitive colliders in detail.

Primitive colliders

Primitive colliders are basic colliders or, in other words, we can say that these colliders are the earliest of their kind. There are three primitive colliders in Unity3D, and apart from these three colliders, Unity also provides Mesh Collider, which helps us when we need to provide a collision shape for a complex shape.

Types of primitive colliders

As shown in the following figure, there are three primitive colliders: Box Collider, Sphere Collider, and Capsule Collider.

Types of primitive colliders

We will learn the uses and types of primitive colliders in detail with examples.

Box Collider 3D

Box Collider contains the cube shape and can be implemented for cube-shaped game objects, such as boxes, walls, and doors, that resemble the shape of a cube.

Example – implementation of Box Collider

Here, we will see how we can implement Box Collider in the following scene by performing the following steps:

  1. Let's create a new scene as shown in the following screenshot:
    Example – implementation of Box Collider
  2. Select the Cube option by navigating to GameObject | Create Other as shown in the following screenshot, and then open the Inspector panel:
    Example – implementation of Box Collider
  3. In the following screenshot, you will see the Box Collider checkbox inside the Inspector panel; make sure it is checked:
    Example – implementation of Box Collider
  4. In the Inspector panel, select the Is Trigger checkbox to make it trigger the collider and fire trigger events.
  5. We can decide its shape and size using the dimension here; it should be relative to the game object transform and size.

    Note

    Material

    Box Collider has the Material property that determines the friction and bounciness of the game object, but it is irrelevant if we choose the Trigger Collider.

In the previous example, we learned how we can implement Box Collider for cube-shaped objects.

Box Collider 2D

For a 2D game object, Unity provides Box Collider 2D to handle the collision. This collider is of the rectangle shape and can be implemented on a sprite with given dimensions and coordinates. Like the 3D Box Collider, this collider too possesses the Is Trigger and Material properties along with the Size and Center properties.

Sphere Collider 3D

For game objects that have the shape of a sphere, we use Unity3D's Sphere Collider.

Example – implementation of Sphere Collider

Here, we will see how we can implement Sphere Collider in the scene using the following steps:

  1. Create a new scene.
  2. As shown in the following screenshot, go to GameObject and select Create Other where you will get a drop-down list; now, select a Sphere game object and open the Inspector panel:
    Example – implementation of Sphere Collider
  3. Inside the Inspector panel, you will see Sphere Collider; make sure Sphere Collider is checked:
    Example – implementation of Sphere Collider
  4. In the Inspector panel, check Is Trigger to make it trigger a collider and fire trigger events.
  5. We can decide its shape and size using the dimension and radius; here, it should be relative to the game object transform and size.

Circle Collider 2D

For 2D objects, we implement Circle Collider instead of Sphere Collider for circle-shaped game object with a given dimension and coordinate. It has all properties which are in Sphere Collider such as Is Trigger and Radius.

Capsule Collider 3D

Capsule Collider is used for capsule-shaped game objects. This is commonly used for creating a character game object.

Example – implementation of Capsule Collider

Here, we will see how we can implement Capsule Collider in the scene using the following steps:

  1. Create a new scene.
  2. As shown in the following screenshot, select the Capsule game object and then open the Inspector panel:
    Example – implementation of Capsule Collider
  3. Inside the Inspector panel, you will see Capsule Collider; make sure that it is checked.
  4. In the Inspector panel, check Is Trigger to make game object trigger a collider and fire trigger events.

    Our capsule object will look as shown in the following screenshot:

    Example – implementation of Capsule Collider
  5. As shown in the following screenshot, we can decide its shape and size using the dimension and radius; here, it should be relative to game object transform and size:
    Example – implementation of Capsule Collider
  6. Select Height as the length of the capsule body.
  7. As shown in the following screenshot, give a direction relative to the game object:
    Example – implementation of Capsule Collider

    By choosing X-Axis, Y-Axis, or Z-Axis, we give a direction to the game object.

Mesh Collider

Using mesh, Mesh Collider defines the shape of a collision. Although we use it for accurate shape definition for collision, this is expensive in terms of performance. We should avoid the use of Mesh Collider, and wherever we can, use primitive collider (Box, Sphere, and so on).

Note

Mesh Collider should be avoided wherever possible by using complex colliders in order to optimize the performance.

Example – implementation of Mesh Collider

Here, we will see how we can implement Mesh Collider in the scene using the following steps:

  1. Create a new scene.
  2. Download or use any existing assets you have. I have downloaded a free spider asset from Unity3D's assets store.
  3. Import the assets in the scene; and you will see them appear in the scene. As shown in the following screenshot, you can see your spider:
    Example – implementation of Mesh Collider
  4. Now, as shown in the following screenshot, we will apply Mesh Collider. Navigate to Component | Physics | Mesh Collider:
    Example – implementation of Mesh Collider
  5. As shown in the screenshot, select the mesh where we have to implement Mesh Collider:
    Example – implementation of Mesh Collider
  6. As shown in the following screenshot, in the Inspector panel, you will see a Is Trigger checkbox, which is unchecked by default. Check Is Trigger to make it trigger a collider and fire trigger events:
    Example – implementation of Mesh Collider
  7. We can mark Convex as true if we want this game object to collide with another Mesh Collider.
  8. Give a direction relative to the game object:
    Example – implementation of Mesh Collider

As shown in the preceding screenshot, by selecting the game object, we open the Inspector panel and we can give a direction.

Polygon Collider 2D

For 2D objects where the shape of the game object is irregular, Polygon Collider 2D is used. To edit the collider shape, drag the sprite asset onto the Polygon Collider 2D component in the Inspector panel, hold the shift key and try to edit the vertex or edges.

Example – implementation of Polygon Collider 2D

The following steps will guide you through to implement Polygon Collider 2D:

  1. Create a new scene and name it Polygon Collider 2D example.
  2. In the Hierarchy pane, click on Create and select Sprite from the drop-down list to create an empty sprite renderer. Name this sprite renderer Rock in the Inspector window.
  3. By default, it will be represented by a box that is not appropriate, so we will create a Polygon Collider instead.
  4. Select Sprite collection and click on Open Editor.
  5. Select the Rock Sprite option in the Sprite Collection editor.
  6. Select Collider Type as Polygon.
  7. Switch the Sprite view option to collider edit mode.
  8. Double-click on an edge to add a control point. Click and drag this to position it.

    Note

    We can edit the polygon's shape directly by holding down the shift key as you move the mouse over an edge or vertex in the Scene view. You can move an existing vertex by shift-dragging when the mouse is over that vertex. If you shift-drag while the mouse is over an edge, a new vertex will be created at the pointer's location. You can remove a vertex by holding down the control/command key while clicking on it.

  9. Edit the collider until you get desired result.

Edge Collider 2D

This 2D collider is used where the collision shape for a 2D game object requires precision. Using this collider, a shape is made of line segments. Using the shift and control keys, we can edit the shape of the collider. We can edit Edge Collider 2D in a similar way as shown in the preceding section on Polygon Collider 2D.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.142.2