Unity: OnTriggerEnter vs OnCollisionEnter

Now that we have Collider and Rigidbody assigned to GameObjects, let’s take a look at how they interact in code.

Ian Plumpton
2 min readMay 18, 2021
The laser collides while the enemy triggers

There are two types of built-in method with Unity that allow us to use collisions and triggers in games: OnTrigger and OnCollision.

The OnTrigger family

OnTrigger has 3 variants (plus 2D equivalents): OnTriggerEnter, OnTriggerExit and OnTriggerStay. Enter will run or trigger the code in the method when a Rigidbody enters the Collider. It takes the information of the other GameObject based on the collider and passes into the method as an argument to be used by our code.

Exit does the same when the Rigidbody exits the Collider and Stay will act like a loop that runs as long as the Rigidbody is in contact with the Collider.

Trigger methods are used when we want objects to pass through each other but register the collision such as laser fire, collectables, etc.

The OnCollision family

Similar to OnTrigger, OnCollision comes in three main flavours. Unlike OnTrigger, OnCollision passes information about the collision to the method as an argument, including collision points, impact velocity etc. This information can then be manipulated within the method to produce the desired outcomes.

Collision methods are used when we want a physical collision between objects, i.e. the ball bouncing in a block-breaker game or cars colliding in a racing game.

Both OnTrigger and OnCollision are powerful methods for crafting the physics of a game but both have different uses. Knowing which to use and when is an important skill for the developer.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ian Plumpton
Ian Plumpton

Written by Ian Plumpton

Software developer in the field of Unity and C#. Passionate about creating and always driven to develop.

No responses yet