Layers and Collision Matrix

By default, if multiple game objects are on the default layer, they will all collide with each other. In that case, we can say everything will collide with everything. In game development, often we need to decide which object will collide with which one. For this, we need to define a layer-specific division of game objects. We put each type of object in different layers. In Collision Matrix, for each new layer, a new column and row are added. Matrix defines the interaction between the layers. By default, a new layer collides with all the other layers. A developer needs to set up the interaction between the layers. Using a correct setup, we can avoid unwanted collision.

An example of a layer-based Collision Matrix

Let's take a look at an example of setting Collision Matrix for layers:

  1. Create a new scene and name it Layer Collision.
  2. Create a box container game object, as shown in the following screenshot:
    An example of a layer-based Collision Matrix

    By default, all the objects will appear on the default layer, as shown in the following screenshot:

    An example of a layer-based Collision Matrix
  3. Add and set up the Box layer for floor and wall game objects. As shown in the following screenshot, by clicking on Layer, we will see a drop-down list of existing layers. We can add a new layer by clicking on Add Layer.
    An example of a layer-based Collision Matrix
  4. As shown in the following screenshot, we have created a new layer Box. By selecting Box, we can specify the layer for all the box game objects.
    An example of a layer-based Collision Matrix
  5. Inside the box container, add a red ball, green ball, and blue ball.
    An example of a layer-based Collision Matrix
  6. Similar to step 3, we add and set up different layers for the balls. As shown in the following screenshot, we have created layers for the red ball, green ball, and blue ball:
    An example of a layer-based Collision Matrix
  7. To set up collision rules for game objects, let's open Matrix. As shown in the following screenshot, navigate to Edit | Project Settings and select Physics:
    An example of a layer-based Collision Matrix
  8. As shown in the following screenshot, a collision table will be shown in the Inspector panel. By default, every object will collide with every another object.
    An example of a layer-based Collision Matrix
  9. Check and uncheck the checkboxes of matrix to set up rules, as shown in the following screenshot. We have set up Collision Matrix for a green ball, red ball, blue ball, and a box.
    An example of a layer-based Collision Matrix

Using the preceding steps, we can set up a layer-based Collision Matrix.

Create a new JavaScript and attach it to the main camera and run the test.

Write the following code inside this JavaScript file:

  #pragma strict
  function Start () {
  }
  function Update () {
  }


function OnCollisionEnter(Collision collisionInfo)
{
  Debug.Log("Detected collision between " + gameObject.name + " and " + collisionInfo.collider.name);

}

function OnCollisionStay(Collision collisionInfo)
{
  Debug.Log(gameObject.name + " and " + collisionInfo.collider.name + " are still colliding");
}

function OnCollisionExit(Collision collisionInfo)
{
  Debug.Log(gameObject.name + " and " + collisionInfo.collider.name + " are no longer colliding");
}

Using the preceding script, you can check which objects are colliding with one another and for how long.

Collision Matrix and a script

Let's see how we can ignore collision for specific objects using a script. Often, in interactive development, we need to set up rules for collision between objects. For example, we need to set up a rule where projectiles should not collide with the objects shooting them. Although, using custom layer-based collision on GUI, we can define a layer-based game objects collision rule, we also can handle it using a script. We can use IgnoreCollision Physics of Unity3D for creating Collision Matrix.

Note

Limitation of IgnoreCollision

Collision Matrix does not store the state when saving the scene and we can apply only on active game objects.

An example of a script-based Collision Matrix

The following is the example of a script-based Collision Matrix:

  1. Create a new scene and name it Script-Collision-Matrix.
  2. Create a wall using Plane.
  3. Create a Bullet game object.
  4. Create a shooting object. Now, we need to set up a rule that a bullet should not collide with the shooting object.
  5. Create a script and attach it to the main camera object. Add the following code to the script:
    var bulletPrefab : Transform;
    function Start () {
        var bullet = Instantiate(bulletPrefab) as Transform;
        Physics.IgnoreCollision(bullet.collider, collider);
    }

Run and test the script, you will see that a bullet is ignoring collision with the shooting object.

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

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