Unitys OnCollisionEnter vs OnTriggerEnter

Objective: Explain the difference between Collision and Trigger events and when to use them.

Before we get into the code we’ll look at a basic example showing the differece between using the collision and trigger. I have place a cube in front of the player in line of where the lasers will shoot. The cube has been scaled along the x-axis from 1 to 5 giving a wall. The box collider is set to IsTrigger.

Wall cube with active IsTrigger

As you can see, the lasers pass through the wall. With the IsTrigger disabled the following behaviour is applied.

Wall cube with disabled IsTrigger

With the IsTrigger disabled the wall no longer allows the lasers the passthrough making the wall reflective making the lasers behaviour erattic and uncontrollable. The IsTrigger allows the wall before a trigger collider where the disabled IsTrigger makes the wall a hard surface collider.

OnTriggerEnter allows the game object the identify the other game object it is trigger colliding with allowing manipulation of the game objects through code such as destroy the wall or lazer prefab OnTriggerEnter. This would also be useful in games such as car racing where a finish line would be set to a trigger and the other cars would be set as collisions allowing the player to cross the line but crash into the other cars.

In the next post we will look at script communication using Unity

Developer