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.
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.
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.
Here, we will see how we can implement a compound collider in the scene using the following steps:
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:
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 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.
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.
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.
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.
The following events are called:
When do we use Trigger Collider? As shown in the following figure, mainly, we use Trigger Collider for two scenarios:
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; } }
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; }
18.218.78.102