Nonprimitive colliders

Nonprimitive colliders originated from primitive colliders. Especially for vehicles, Unity provides Wheel Collider, and for terrains, a Terrain Collider component. By combining various primitive colliders, we create compound colliders.

Types of nonprimitive colliders

As shown in the following figure, nonprimitive colliders are specific for wheel objects and terrains. Using a Wheel Collider, we can easily provide a collision shape for vehicles. Similarly, using Terrain Collider collision implementation for different shapes becomes easy.

Types of nonprimitive colliders

Wheel Collider

This is a nonprimitive collider used for wheels of a vehicle. Wheel Colliders have motorTorque, brakeTorque, radius, and steerAngle properties. Using a friction model, Wheel Colliders are able to give a realistic effect.

Example – implementation of Wheel Colliders

Here, we will see how we can implement a compound collider in the scene using the following steps:

  1. Create a new scene.
  2. In the Hierarchy pane, click on Create and select the Cylinder game object. Open the Inspector panel.
  3. Add Wheel Collider from Component, as shown in the following screenshot:
    Example – implementation of Wheel Colliders
  4. Play with the properties to give a realistic effect.

Let's open the Inspector panel. We will see some different properties; here, using Mass, we decide the mass of the wheel game object, which must be greater than 0. In this example, I have used 1. We can set the radius by using the Radius property that defines the radius of the wheel, measured in local space. Have a look at the following screenshot:

Example – implementation of Wheel Colliders

As shown in the preceding screenshot, there are other properties that are also used to give a realistic wheel effect; for example, the wheel's collision detection is performed by casting a ray from the center (http://docs.unity3d.com/ScriptReference/WheelCollider-center.html) down the local y axis for which we use the Center property. We can extend the wheel's radius downwards by the suspensionDistance (http://docs.unity3d.com/ScriptReference/WheelCollider-suspensionDistance.html) amount. By changing forwardFriction (http://docs.unity3d.com/ScriptReference/WheelCollider-forwardFriction.html) and sidewaysFriction (http://docs.unity3d.com/ScriptReference/WheelCollider-sidewaysFriction.html) based on what material the wheel is hitting, we simulate different road materials.

Static collider

Static collider is used wherein a collision movement is not required. A static collider contains a nontrigger collider without a Rigidbody. This type of game object that does not have any Rigidbody will not get affected by Physics. Static colliders are mostly used to create boundaries or blockages.

Rigidbody Collider

The Rigidbody Collider is exactly the opposite of a static collider; it is a collider with Rigidbody components. Rigidbody Collider will collide with a static collider to create collision events and will get influenced by Physics.

Kinematic Rigidbody Collider

A Kinematic Rigidbody Collider is a collider that has the Is Kinematic flag true. Mostly, it is used where animation is required. Also, the Kinematic Rigidbody Collider is not influenced by the script.

Trigger Collider

The Trigger Collider is an invisible collider that fires events without any physical interaction. We can define nonphysical area where Trigger Collider will fire event on interaction with game object.

Note

Triggers should be used where a quick response is required. For games such as Tower Defense where a quick response is required, Trigger Colliders are very useful.

The following events are called:

  • OnTriggerEnter: This event is called when the object just enters
  • OnTriggerStay: This event is called when the object is inside the trigger
  • OnTriggerExit: This event is called when the object leaves the trigger

When do we use Trigger Collider? As shown in the following figure, mainly, we use Trigger Collider for two scenarios:

Trigger Collider

An example of proximity triggers

During the development of one of my games, I had to create a game play where when a player reaches the door, the door should open. In this case, I needed to put a Trigger Collider in front of the door. When the player reaches the door, it fires the OnTriggerEnter event where I was executing game logic accordingly:

    var  isInRange:Boolean = false;

function OnTriggerEnter(col:Collider)
{
  if(col.CompareTag("Player"))
  {
      // Write here logic for open the door.
   }
}

Similarly, we can use OnTriggerStay and OnTriggerExit:

function OnTriggerStay(col:Collider)
{

  if(col.CompareTag("Player"))
  {
isInRange = true;
   }
}
function OnTriggerExit(col:Collider)
{
  if(col.CompareTag("Player"))
  {
isInRange = false;
   }
}

An example of radius triggers

In some games such as Tower Defense, we are required to spawn troops or shoot enemies in a range; hence, we create a radius. When the game object enters within the defined radius, OnTriggerEnter events get fired:

    var  isInRange:Boolean = false;
function OnTriggerEnter(col:Collider)
{
  var spawn:SpawnPoint = col.GetComponent(SpawnPoint);
  if(spawn){
    spawn.shootBullets();
  }
}

Similarly, we can use OnTriggerStay and OnTriggerExit:

function OnTriggerStay(col:Collider)
{
  isInRang = true;
}
function OnTriggerExit(col:Collider)
{
   isInRang = false;
}

Note

Warning

Trigger Colliders respond to raycasts. Make sure your triggers are set to the Ignore Raycasts layer.

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

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