Singapore-MIT GAMBIT Game Lab spacer
  CMS MIT
spacer New Entries Archives Links subheader placeholder
Updates
left edge
pinstripe
Triggers and colliders

I've been trying to import the Abandon meshes into Unity, but it appears I need a new version of Maya installed, so while I'm waiting I've been fiddling with the Chaser script, which makes random objects chase the player through the maze.

When I first implemented the chasers, I simply had them chase the player when the player bumped into them. I had to add a coroutine to make them wait first, or they would simply have stuck to the player immediately, but this was just a temporary fix; the goal was to have the chasers follow the player when the player got near them, rather than actually touched them. This is when I began to have trouble with triggers and colliders.

A Collider component attached to an object uses Unity's physics functions to detect when the object has hit another object and react accordingly. Each Collider has a boolean variable called isTrigger, which designates it as a trigger. An object has separate functions for what to do if it hits a collider and what to do if it hits a trigger. The problem is that when a collider is designated as a trigger, it no longer seems to perform the usual collider function of making sure the object bumps into other objects instead of ghosting through them. I've tried attaching two colliders to an object and making only one a trigger, but this gets complicated very quickly and I'm not sure it really works.

Part of the problem was that I had already designated the halo as a trigger, so I had to make a distinction between the player triggering the halo and triggering a chaser. I tried making the player the trigger, which failed because I still needed it to have collider functionality. This is the point at which I tried giving the avatar two colliders, which caused the problems noted above.

At last I got it to work by making the chaser a trigger and telling the player to send the chaser a signal to chase it when it hit the trigger. Then I went back to the original Abandon game for reference and realized that the range at which the player triggered a chaser was going to have to be variable, making it necessary for the player to be the trigger after all.

To avoid the double-collider problem, I took advantage of another change I would have to make to match the original version: attaching the point light to the player rather than the halo. While it's possible to attach a light component to an object,m which was what I had done with the halo, in this case I found it more convenient to create a point light and make it a child of the player. I then made the point light the trigger, which also allowed me to simultaneously adjust the light level and the trigger range, just as the original game did. The biggest problem after that was figuring out how to adjust the collider radius of the trigger from a script, since only some types of colliders (such as SphereCollider and CapsuleCollider) have a radius at all. I made it work by casting the collider type:

((SphereCollider)collider).radius = light.range;

Also, this is a relatively minor hassle, but it's worth knowing: while two colliders bumping into each other causes both to call OnCollisionEnter, if a collider bumps into a trigger, the other object (not the trigger) is the one that calls OnTriggerEnter.

right edge
bottom curves