This is the 22nd day of my participation in the August More Text Challenge

The previous article “Unity basic Object System – Collider properties introduction”, introduced the various commonly used collider properties panel, this article we take a look at how to use the collider.

Collider concept

The Collider component defines the shape of the object for use in physical collisions. Colliders are invisible, their shape does not have to be exactly the same as the mesh of the object, and in fact, rough approximations are often more effective and hard to detect while the game is running.


Collision and triggering

The essential difference Is whether the Is Trigger attribute Is checked on the collider component:

That Is, when two colliders are close to each other without the Is Trigger attribute checked (contact in life, next to each other), the two objects will collide. For example: when a ball falls to the ground, it bounces up. This is where two colliders collide and force each other. This is also the case in Unity, where two objects collide and force each other apart

If one of the objects has the Is Trigger attribute checked, then the two objects will not collide. For example: A car hitting a tree in PubG will go straight through it as if there were no tree at all.


Code trigger condition

Unity provides code to detect triggers and collisions, respectively:

  • Trigger information detection:
    • OnTriggerEnter(Collider Other) When entering the trigger
    • OnTriggerExit(Collider Other) When exiting the trigger
    • OnTriggerStay(Collider Other) when a linger trigger
  • Collision information detection:
    • OnCollisionEnter(Collision collisionInfo) When entering the collider
    • OnCollisionExit(Collision collisionInfo) When exiting the collider
    • OnCollisionStay(Collision collisionInfo) when lingering collider

The above six functions, and one of the corresponding 2D collider functions, add 2D after the function name, can listen to the 2D collider trigger.

Collision function automatic execution conditions:

  • Both objects have colliders
  • Rigidbody is attached to at least one object
  • Is Trigger Is not checked on either object

Trigger function automatic execution conditions:

  • Both objects have colliders
  • Rigidbody is attached to at least one object
  • At least one Is Trigger

Code examples:

public class ColliderTest : MonoBehaviour 
{
 
    // -------------- collider --------------
    private void OnCollisionEnter(Collision collision)
    {   
       Debug.Log("Collider OnCollisionEnter");
    }
 
    private void OnCollisionStay(Collision collision)
    {
       Debug.Log("Collider OnCollisionStay");    
    }
 
    private void OnCollisionExit(Collision collision)
    {       
       Debug.Log("Collider OnCollisionExit");
    }
    
    // -------------- trigger --------------
    / / check isTrgger
    private void OnTriggerEnter(Collider other)
    {       
       Debug.Log("Trigger OnTriggerEnter");
    }
 
    private void OnTriggerStay(Collider other)
    {
       Debug.Log("Trigger OnTriggerStay");
    }
 
    private void OnTriggerExit(Collider other)
    {
       Debug.Log("Trigger OnTriggerExit"); }}Copy the code