Search Unity

Question Particle System and Time Scaling

Discussion in 'Editor & General Support' started by mattbru2, Mar 27, 2023.

  1. mattbru2

    mattbru2

    Joined:
    Mar 3, 2021
    Posts:
    18
    Hello,

    I'm using a particle system TriggerModule and registering a collider against it to receive OnParticleTrigger() callbacks so I can tell when a particle is inside the collider.

    I also have allowed the user to scale simulation time (setting Time.timeScale). So the player can set the time scale to 2x or 3x to simulate faster. Note that all of my code is done in FixedUpdate as I need repeatable, consistent playback of the simulation, regardless of framerate. I essentially set a bool flag in OnParticleTrigger so that on the next FixedUpdate call, I can make a decision on if the collider currently contains a particle.

    At 1x, I seem to get consistent behavior. While the particle system is inside the collider, I get what seems to be a OnParticleTrigger() callback every timestep (0.02s FixedUpdate rate).

    At 2x/3x/etc, it seems like the particle system collision triggering isn't running every Physics step (FixedUpdate()). I'll get a call back at 0.04, 0.08, 0.12 as if the detection process is scaled. What this means is that I cannot use the bool flag described earlier to tell if there is currently a particle inside the collider since OnParticleTrigger isn't called every timestep.

    But maybe I'm getting something wrong here?

    1. Does anyone know anything about time scaling and particle systems that could help me?

    2. Is particle system collision done at the fixed timestep rate, as part of the physics loop described here https://docs.unity3d.com/Manual/ExecutionOrder.html ?
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    the particle update is not done at fixed update frequency but you can call ParticleSystem.Simulate yourself in FixedUpdate if you need it to be in sync with that update rate.
     
  3. mattbru2

    mattbru2

    Joined:
    Mar 3, 2021
    Posts:
    18
    Thanks for your reply. What is the particle system update linked to when not manually called via ParticleSystem.Simulate?
     
    Last edited: Mar 28, 2023
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    it has its own fixed update frequency, defined by Max Particle Timestep in the Time project settings. But if the delta time isn't larger than that value, it just does an update based on whatever Time.deltaTime is for each frame.
     
  5. mattbru2

    mattbru2

    Joined:
    Mar 3, 2021
    Posts:
    18
    So it sounds like I have 2 options if I'd like my particle systems to run every FixedUpdate (0.02s):

    1) Set `Max Particle Timestep` to 0.02s, noting that it may run *faster* than that if the frame time is smaller?

    2) Keep a reference to all my particle systems and manually call `Simulate` every FixedUpdate. Could you explain a bit more about what the fixedTimeStep flag does there?


    Also, does 'Maximum Allowed Timestep` play into this at all?
     
    Last edited: Mar 28, 2023
  6. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
  7. aymen-Wiz

    aymen-Wiz

    Joined:
    Apr 30, 2014
    Posts:
    1
    Why does Unity Update its particle systems based DeltaTime + Clamps it using the MaxParticleTimestep ? woudn't that still lead to unexpected behaviors ? (such as when using DeltaTime rather than a FixedDeltaTime, accumulations happen overtime of very small values changes which can change the particle system appearance, particular example is leading to bursting or very weird "shapes") ? if not how does unity mitigate this ?
     
  8. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    MaxParticleTimestep is used to ensure the simulation doesn’t degrade too much if the time steps get too large. if they do, it does multiple smaller updates. It will keep things “approximately” the same regardless of delta time but it’s not exact.

    It’s been done like this for a lot of years, i couldn’t say if it’s the best way.

    So yeah basically you’re right :)