unity ontrigger code example
Example 1: unity on trigger enter
//for unity c#
private void OnTriggerEnter(Collider other)
{
// code
}
Example 2: Ontriggers unity
//For 3D triggers remove the 2D (e.g. OnTriggerEnter(Collider collision)
/*There are three ways of detecting a Trigger:
-OnTriggerEnter(2D): Activated when player Enters the Collider
-OnTriggerStay(2D): Activated when player Stays in the Collider
-OnTriggerExit(2D): Activated when player Exits the Collider */
public GameObject fire;
//This is for Specific Objects:
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject == fire/*Name of Object*/)
{
//Your Code goes in here
Destroy(Player);//this is an example
}
}
//This is for Specific Tag:
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == toHot/*Name of Tag*/)
{
//Your Code goes in here
Destroy(Player);//this is an example
}
}
//This is for Specific Layers:
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.layer == 3/*Layer Index/Number */)
{
//Your Code goes in here
Destroy(Player);//this is an example
}
}
Example 3: ontriggerenter
private void OnTriggerEnter(Collider other)