Trigger Events

In Unity 5

Good evening gents and ladies.  This tutorial is one of my favorites, trigger events.  Trigger events form the backbone of basic object interaction within Unity.  Today, I’m going to show you a bit of the code behind trigger events and also an example of one in action.


The screen above shows a sphere collider.  Really, any collider can function as a trigger collider when we check the Is Trigger option.  Setting a collision box to trigger frees it up from any physics based interaction.  It will pass through objects and other colliders as if they weren’t there.

Why might this be useful, you ask?
  • Well, first I should say that you can pair up a larger trigger collider with a smaller regular collider, so the object can still collide with world objects, but can still check for triggers, so an object need not be completely useless.
  • Second, trigger events are extremely useful.  If you want ‘something’ to happen to either the trigger object or its intended target in 3D or 2D space, then you may want a trigger collider.
  • If you want a projectile to pass through space unrestricted to hit its intended target, then you may want a trigger collider.

Trigger colliders make use of three main methods, which you need to manually include inside a code component.  I’ve taken the liberty of including a snippet of a C# class called ProjectileDamageObject.cs.

  • OnTriggerEnter() runs when an object passes into the collider.  This will run once when an object enters and will not trigger again on that same object until said object leaves and then enters again.  This is useful as a general OnCollision() type of method.
  • OnTriggerStay() calls every frame in which an object exists inside of the collider.  Very useful, though for different purposes.  Taking damage in lava might be a good idea for this one.  I’ve used on trigger stay if I want to automate a process as long as the player is inside of a particular area.
  • OnTriggerExit() is a less used function, probably because most things you can do with this can be done with OnTriggerEnter(), but it does have its uses.  I used this to check how many game objects have left a particular area so I could determine whether or not they needed to respawn.


The inspector below shows my trigger collider and the aforementioned class that deals damage to an enemy when it comes into contact with one.  Trigger checks are useful because I can check for an object with the Enemy tag or the Player tag.


That’s all I have today, so I’ll see you next time on another tutorial.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s