Search Unity

is OnParticleTrigger() function from the particle system supposed to run every frame?

Discussion in 'Scripting' started by coder091, May 16, 2021.

  1. coder091

    coder091

    Joined:
    Feb 16, 2021
    Posts:
    23
    I've been trying to use OnParticleTrigger which is set to a callback, then I do stuff but it seems that OnParticleTrigger runs every frame even though i'm not triggering anything.

    I tried searching on forums, but no proper answer is found I tried isolating the particle system to make sure nothing is touching the particles, also didnt work

    any ideas?
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    It runs every frame but if no particles met the conditions set in the trigger module, then GetTriggerParticles will return 0. Call that inside the callback and use it to know if any particles are interacting with the colliders in the way you are interested in.

    (Eg if no particles were inside the specified colliders)
     
  3. coder091

    coder091

    Joined:
    Feb 16, 2021
    Posts:
    23

    Thank you for clarification, i love you
     
    fusefactory likes this.
  4. Rand_D

    Rand_D

    Joined:
    Oct 24, 2017
    Posts:
    44
    @richardkettlewell Why is it designed to be that way? It should call back only if there is an actual collision happen or every frame we have to perform a check. Also for non persistent objects that contain the collider (get instantiated and deleted frequently in the scene) it makes it difficult to control be behavior with trigger particle.

    Is there anyway to make trigger particle behave like the normal OnTriggerEnter(Collider)
     
    Last edited: Oct 29, 2022
  5. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Good point, I think I misspoke. I think it shouldn’t get called if all subscribed events have no particles. If you think it isn’t working like that please let me know.
     
  6. mklasson

    mklasson

    Joined:
    Oct 30, 2018
    Posts:
    28
    Using 2021.3.10 it gets called every frame even if there are no matches.

    upload_2022-11-1_11-52-29.png
    still calls OnParticleTrigger every frame, despite 0 Enter particles.

    A lesser issue is that if Prewarm is true then OnParticleTrigger gets called before Awake. Maybe this is unavoidable? In any case it's easy to work around.
     
    richardkettlewell likes this.
  7. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Hmm, part of the internal "awake" process performs the prewarm. This is no doubt what's calling OnParticleTrigger. I think maybe it's ok to respond to events in the callback here, but probably some stuff will be invalid. It's probably risky to change it, but also risky as it is now...! So probably best to leave it as-is.

    I just checked the code and you're right. I would have done this sooner but it was Saturday when i last replied :)
    I will fix this so we only call it when there are trigger events, thanks for letting us know!
     
    Rand_D and mklasson like this.
  8. Rand_D

    Rand_D

    Joined:
    Oct 24, 2017
    Posts:
    44
    Please get me a head up when it will be delivered. Im using 2021 LTS atm. This fix will smooth out some part of my game mechanics a lot because I have to do quite a few workarounds for particle trigger atm.
     
  9. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    It'll be hard for me to do that without a bug report. That's our usual flow for keeping you informed of what's going on.
    The fix is heading to our latest version (2023.1), but it won't be backported by default without a bug report from a customer saying they need it in that version.

    If you could make a bug report, I'll try to get it organised. You can link to this thread in the report.
     
  10. Rand_D

    Rand_D

    Joined:
    Oct 24, 2017
    Posts:
    44
    richardkettlewell likes this.