Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Subclassing Collider?

Discussion in 'Scripting' started by sleepyCat, Jul 3, 2005.

  1. sleepyCat


    Jun 3, 2005

    for the "Unity Roleplaying Framework" I am thinking about, I would like to notify non player characters (or better their controlling AI object) about "events" (e.g. another npc shouting for help). The "events" will be depicted by creating a temporary (sphere) collider/trigger for each of them.

    So if I want to tell the game object (aka the npc) entering the trigger with what "kind of event" (a shout for help, somebody pick pocketing someone etc.) it just collided, how do I do it?

    Can I subclass Collider to "EventCollider", which will get a group of additional properties e.g.

    Code (csharp):
    2. bool isShoutForHelp;
    3. bool isAttack;
    4. ...
    5. Object actuator;
    6. Object receiver;
    7. [code]
    9. The booleans depict the "type of event" which has to be analyzed by the ai object of the npc entering the trigger. Actuator is the game object which created the event, and the receiver is the game object which is the "victim" of the event.
    11. If subclassing Collider is possible, how would I tell Unity to use my EventCollider?
    13. If this is not possible I thought about something like that:
    15. As the trigger inherits from Component, I give each "type of event" a Tag (e.g. "scream for help") and let the npc's AI query the Tag string. These Tags have to be created "beforehand" via the Tag Manager. Can I create a couple of Tags where each Tag can be used by multiple Colliders or is this a strict 1:1 relationship?
    19. I plan for a hundreds (if not thousands) events per "time increment", are Colliders "lightweight" enough (on an "algorithmical scale" not on a "practical" ) for such heavy use?
    21. Thanks, Alex
  2. Joachim_Ante


    Unity Technologies

    Mar 16, 2005
    You can't derive from Collider. You can only derive from MonoBehaviour.
    (In javascript this happens transparently)

    You can just attach your script to the same game object as the collider and you will receive collision events.

    If you have events that need to be sent out only once the best way to it is to use:
    Physics.OverlapSphere ()
    This gives you back an array of colliders touching or inside the sphere.

    When you want to notify players about entering a sphere, which is persistent, you should create a primitive collider and click the isTrigger checkbox.
    This will give you OnTriggerEnter / OnTriggerExit callbacks in the script attached to the same game object.

    Performance wise i think the main thing is limiting the amount of trigger messages you send out to less than 100 a frame.