What is collider In Unity | What Is Triggers In Unity
In this tutorial you will learn about colliders in unity. Unity game engine have various collider and all have different colliders. Also we will learn some script about manipulating priorities of collider. Now lets start.
Here are list of colliders (3D Colliders) that we will going to teach-
1) BoxCollider
2) CapsuleCollider
3) MeshCollider
1) BoxCollider
As its name tells, BoxCollider component creates a 6 side area with collision property. It is perfect for cube and cuboid type shape.
2) CapsuleCollider
This collider create a capsule border which will protect any object with collider from passing through.
3) MeshCollider
This is a complicated collider and it make app faces of mesh solid. But you can not use rigidbody with this collider. If you want to use rigidbody with MeshCollider then you have to do atleast one work, either check MeshCollider as convex or set rigidbody kinematics true.
Now lets learn some Script (c#) about changing property of collider.
If you are unsure that any object have BoxCollider or CapsuleCollider or MeshCollider then you can access them like below.
myObject.GetComponent<Collider>();
Collider Component can access any of the collider property.
Use of Colliders
In unity collider is a most useful property which every beginner should understand. When a beginner start unity and then he/sher learn about Rigidbody then they used to think that rigidbody means a body which is rigid and it will collider with any body. Bow the add rigidbody component to a object and when they run game, that gameObject with rigidbody falls and pass through terrain or any other floor. So you must attach a collider so that your object do not pass through other objects. Rigidbody only give a object physical properties like mass, drag, angular drag.
In making triggers
Triggers are one of the most useful system of unity. Now lets learn something about trigger. A gameObject with trigger can detect external objects which pass through them.
Now if you are making a coin system then you can use trigger function. Lets consider you have a player and you want to add coin when player touch them. First set coin tag as Coin and add a collider with isTrigger checked.
int coinCount;void private void OnTriggerEnter(Collider collision)
{if (collision.gameObject.tag == "coin")
{
coinCount ++;
Destroy(collision.gameObject);
}}
Above method will destroy the coin gameObject and add a coint to player.
Detecting a car reached to final
Make a gameObject with collider and IsTrigger enabled. Now hide renderer so that it will become invisible but active. Now set it’s tag as “Final”. Make sure your can Have a mesh collider in root.
Now in your CarController script write these codes.
void private void OnTriggerEnter(Collider collision)
{if (collision.gameObject.tag == "Final")
{
Debug.Log("Car reached to final destination");
RaceComplete();
}}
Use of Trigger in making ammo system
You are making a shooting game and want to make ammo system with meshes then you can use triggers. But it is good to make ammo system with RayCast but if you want it with colliders then use below codes in your enemy script. Lets say You have EnemyController script and ammo object is tagged as “Ammo”
int health =100;
void private void OnTriggerEnter(Collider collision)
{if (collision.gameObject.tag == "Ammo")
{
if(health >0){
health -= 10;
}
}}
Now everytime ammo touch the enemy, their health be decreased by 10 points. Now you can detect when their health become zero.
Use of Trigger in making fire system
This thing is rare but if you want to make a fire in Unity3D which hurt character then your work will be done by triggers. When player touch the fire, a function will be called that will decrease health of player. Lets set the tag of fire object as Fire
int health =100;
void private void OnTriggerEnter(Collider collision)
{if (collision.gameObject.tag == "Fire")
{
if(health >0){
health -= 10;
}
}}
You can use Trigger function in a lot of ways. Once you understand how it work, you can make many things in unity. OnTrigger functions work only if either object have isTrigger enabled.
So, these are some knowledge and tips about Unity Collider and triggers. We hope you learnt something from here. If you are a beginner then You must learn triggers as soon as possible. This thing will do a lot of work.