OnTriggerEnter
Hello Find Nerd Readers,
While making even the simplest game you have to use triggers and collisions. In this blog we are targeting Triggers.
Q1: What are Triggers?
Ans: Triggers in Unity are Collider which does not reflect any physical force while collision with any object but they do throw an event OnTriggerEnter(Collider collider) by overriding this method in Monobehaviour class you can get even on Collision.
Q2: Why to Use Triggers?
Ans: When you have to keep track of events which happen at a particular position in game but you cant show any visible object to check collision with so that you know that even occur, you need to use Triggers.
Q3: How to make Unity Collider a Trigger Collider?
Ans: Just Enable that check bool in Collider Component which says Is Trigger.
Q4: How to get TriggerCollision Event in your Code?
Ans:
void OnTriggerEnter(Collider coll){
// Here we code what to do if you collide with a trigger area.
Debug("Entered In Trigger: " + coll.gameObject.name);
}
void OnTriggerStay(Collider coll){
// Here we code what to do if you remain inside a trigger area.
Debug("Currently in Trigger: " + coll.gameObject.name);
}
void OnTriggerExit(Collider coll){
// Here we code what to do if you exit from a trigger.
Debug("Exited from a Trigger: " + coll.gameObject.name);
}
Note: coll object contains the collider reference of a Trigger Object.
Q5: Why my Trigger is not working as expected?
Ans: Make sure in Scene editor that trigger collider is setup properly like this.
In case you are using it on an object which have to show physical boundaries as well as trigger boundary you can setup two collider at same time one triggered for trigger boundary and one non triggered for physics boundary but make sure triggers boundary is a little bit bigger than physical one.
Q6: Trigger Collider setup properly still not working?
Ans: Trigger Colliders in unity works only with primitive colliders if you are using Mesh Collider then your Trigger Collision system will not work.
0 Comment(s)