Add a collider to object T (always create a 2D collider when creating a 2D game) If is Trigger is checked, it is the Trigger.

What happens when an object meets an object in game development (for example: enemy meets enemy;

Collider method

private void OnCollisionEnter2D(Collision2D collision) { switch(collision.transform.tag) { case "invincible": invincibleprop = true; invincibleTime = 6f; break; case "ironBase": PlayerManage.Instance.ironBase = true; PlayerManage.Instance.ironBaseTime = 10f; break; case "resuse": PlayerManage.Instance.lifeValue2++; break; default: break; }}Copy the code

Private void OnCollisionEnter2D(Collision2D Collision) // When the mount object enters the object (or encounters it, I think)

This method must add collider components to both objects; Is Trigger is not checked, the transform should be added when calling the footstep property on the object when writing collision;

Generally I will add labels to the different preforms to judge;

Trigger method

private void OnTriggerEnter2D(Collider2D collision) { switch(collision.tag) { case "Tank1": if (! isPlayerBullect) { collision.SendMessage("Die"); Destroy(gameObject); } break; case "Tank2": if (! isPlayerBullect) { collision.SendMessage("Die"); Destroy(gameObject); } break; case "Heart": collision.SendMessage("Die"); Destroy(gameObject); break; case "Enemy": if (isPlayerBullect) { collision.SendMessage("Die"); Destroy(gameObject); } break; case "Wall": Destroy(collision.gameObject); Destroy(gameObject); break; case "Barriar": Destroy(gameObject); break; default: break; }}Copy the code

Trigger method: Private void OnTriggerEnter2D(Collider2D Collision) // Implemented when an object enters an object

When the object mounts the Trigger, the object can pass through the object mounted with the collider method.

I usually use this method for things like bullets, because bullets can go through something, something that can’t go through, let the bullets destroy it;

Supplement:

Objects with colliders cannot pass through each other. Sometimes if you want the main character to be confined to a certain location, you can limit the main character’s movement by placing walls around the collider.