2
\$\begingroup\$

I currently have an enemy that upon collision kills the player with a kill script, I want to add another collider that acts as the visibility radius for said enemy. However, when adding another colldier with a bigger x radius, the collidier still kills the enemy. Is there any way to assign particular colliders specific methods using

OnTriggerEnter2D (Collider2D var)
{
}
\$\endgroup\$
4
\$\begingroup\$

First its good to note the difference between Colliders and Triggers

  • Colliders - generally are intended to represent physics interactions, so objects "colliding with them" should not pass through one another. As opposed to...

  • *Triggers* - as the name implies, "fire" off an event but don't carry the typical physics body interactions

Triggers and colliders also send different events upon interaction, and the same can be said as well for 2D and 3D objects

OnTriggerEnter: 3D | 2D

OnCollisionEnter: 3D | 2D

Knowing this there are a few ways to approach this problem. First like you suggested you could add a second collider to the enemy, and from that new Collider's Component Inspector on the enemy check the isTrigger box marking it as a "Trigger".

isTrigger

That will allow it to instead respond to the OnTriggerEnter Method (if it is a 2D Collider OnTriggerEnter2D)

You could also organize your Enemy GameObject with a hierarchical structure

Enemy Game Object Parent
    - Damage Collider Object Child
    - Sight Radius Object Child

and then adjust the organization of your scripts to only have the kill script on the collider which should kill the player. This way you could use Triggers or Regular colliders.

\$\endgroup\$
  • \$\begingroup\$ I like the object hierarchy approach myself \$\endgroup\$ – SpartanDonut Apr 29 '15 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.