Search Unity

Handling collisions

Discussion in 'Entity Component System' started by e199, Nov 26, 2018.

  1. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    Hey

    Imagine you have a stick entity which has rigidbody component and collider and you have a campfire entity which is a trigger.

    Interaction I want to achieve: When stick enters campfire trigger, then after 1 second I want to add to stick component Flamed. If stick leaves campfire with Flamed component - it still remains on fire for some time.

    Solution I'm thinking about:

    Stick gameObject has monobehaviour attached which attaches component TriggerEnter(Entity trigger) to local entity if rigidbody enters any trigger and that trigger has GameObjectEntity. When rigidbody leaves trigger it attaches TriggerExit(Entity trigger) component.

    Both of those components are removed at the end of frame.

    Then I have system which looks for entities containing TriggerEnter and Flammable and NOT Heating{float} component. Then it looks if trigger entity is Flamed, then it attaches Heating component to entity which has TriggerEnter component.

    Another system looks for Heating components. It simply increases it's value with delta time.

    Another one is looking for Heating components and if it's value is bigger than X (or value of some FlamingMinTime component), then it removes Heating component and attaches Flamed component.

    Last system looks for TriggerExit and Heating components, it simply removes Heating component.
    (For simplicity let's imagine we can't have overlapping flamed triggers)


    What do you think about it?
    How would you approach something like this?
    I prefer to use hybrid approach until pure physics is integrated to unity.

    Any ideas are welcome.
     
  2. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    Just spitballing but I think you could ignore trigger/collider completely just go with a single Flammable component(as well as a Position, GameObjectEntity and CopyTransformFromGameobject components). It would have an OnFire int (in place of a bool), a Radius and Fuel counter that ticks down.

    My system would just loop through each Flammable component and compare its Position to every other Flammable component and check the distance and whether a component was OnFire or not. If a Flammable is near another which is OnFire and within its Radius, that first component is now set to OnFire and given an appropriate Fuel burn time.
     
    Vacummus likes this.
  3. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    It's all good until there is a need to use meshColliders :)

    I need to use them in other cases to trigger something (campfire is just a simple example)
     
  4. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    I think the approach @thelebaron proposed should scale easily for other use cases where you need to trigger something. It would also be more performant and more easily optimizable then using Colliders to detect triggers.
     
  5. e199

    e199

    Joined:
    Mar 24, 2015
    Posts:
    101
    I agree, it will cover simple triggers use case.