- Home /
What is the trigger?
What is it? how to use it?Where I can find a tutorials of that?
Answer by Gruffy · Apr 08, 2016 at 12:35 PM
A trigger is something that is termed that way due to the nature of what happens as a result of interacting with another object. The real word we should start with is intersect. This is what the trigger is effectivley being polled for [alt: questioned over] Its actually arbitrary compared to things like raycasts. They can stay in idle states and will only start running based on a boolean being triggered to true when something "intersects it" so the code is never run until the moment the intersect is caught So every update in the game, the objects with triggers are polled (that word again) and if any returned as true then the code must need firing and Unity will pile all of the OnTriggerEnter/OnCollisionEnter methods into one big base update for all of them during an update loop; any needing action will be caught there and consequently handled. So... Against something like raycasting which is always being cast at every update, trigger based gamobjects need do nothing until they are intersected [triggered] to do so - this can be a very handy thing to save some CPU processing time in some scenarios for example. efge was almost there in providing the link above. This may be of help too as it is from Unity.. Colliders Hope this help some bud, I am also keeping this high level due to the bogged down complexities qwe could go into here as your question asks "what is the trigger"
With respect on how to use it, well... Make a new scene and create two cube gameobjects...
On one game object in your scene, in the inpector for the collider... Check the "IsTrigger" check box (give it a tick)
Now make a script and attach it to your gameobject the one you have just checked the "IsTrigger" for :)
In that script ,remove your update and start loops and add this following code inside the classbody...
void OnTriggerEnter(Collider other)
{
Debug.Log("You have entered the objects trigger area ");
}
void OnTriggerExit(Collider other)
{
Debug.Log("You have exited the objects trigger area ");
}
void OnTriggerStay(Collider other)
{
Debug.Log("You are living inside the objects trigger area ");
}
Now in your scene add for the other cube game object, give it a rigidbody in the inspector - remove the gravity check box tick
Now run your scene and drag this second cube into the first "IsTrigger" one. You will see in the console window a number of debug messages. These only appear when the trigger condition is fired and not before or afterwards.
You may notice we have 3 particular types for the trigger object and what is the need for rigidbody?
If an intersect is to be detected between two objects that are both active in a scene, it seems that at least one should own a rigidbody component. What is weird is that OnTriggerEnter etc comes from MonoBehaviour and not PhysX which may have provided a reasonable answer as to why we need rigidbody { somone on here will know the real technical reason non doubt and it will likely be some property that rigidbody interfaces and MonoBehaiviour takes advantage over- but who knows!?!?)
TriggerEnter is waiting to fire when the trigger is broken/intersects for the first time
TriggerStay is a way to continue updating the gameobject [or something] whilst the intersect has the other gameobject inside its collision area.
TriggerExit is waiting to fire when the object is no longer intersecting with the other
Cheers bud, sorry it a long ol explanation and in fairness, its still very high level. Theres much more and greater depth to go in to here, but at the bottom of the hill climbing upwards, this should give you a fair start. :) Cheers bud
Answer by efge · Mar 14, 2010 at 03:39 PM
The 'Triggers' section of the Box Collider reference explains it.
There are also links to the scripting reference for the trigger messages with examples.
Your answer
Follow this Question
Related Questions
calling buttons in triggers 1 Answer
See if an object is in contact with any number of a certain type of object 1 Answer
How to have multiple colliders intersect without colliding with each other? 1 Answer
How to keep track of multiple gameobjects inside collider 0 Answers
Mathf.PingPong and triggering events 1 Answer