unity oncollisionenter vs ontriggerenter code example
Example 1: unity oncollisionenter2d
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("Collided");
}
Example 2: oncollisionenter unity
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Door")
{
DoorScript script = collision.gameObject.GetComponent<DoorScript>();
script.OpenDoor();
}
}
Example 3: ontriggerenter and oncollisionenter difference
OnTriggerEnter - Is called whenever an object with a collider has passed through an object with an "isTrigger" checked collider. ... OnCollisionEnter - Is called whenever an object with a collider has collided with another object that contains a collider.