Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Subclassing Collider?

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

  1. sleepyCat

    sleepyCat

    Joined:
    Jun 3, 2005
    Posts:
    39
    Hi,

    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):
    1.  
    2. bool isShoutForHelp;
    3. bool isAttack;
    4. ...
    5. Object actuator;
    6. Object receiver;
    7. [code]
    8.  
    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.
    10.  
    11. If subclassing Collider is possible, how would I tell Unity to use my EventCollider?
    12.  
    13. If this is not possible I thought about something like that:
    14.  
    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?
    16.  
    17.  
    18.  
    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?
    20.  
    21. Thanks, Alex
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,049
    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.
     
unityunity