Search Unity

Question Call function when particle gets destroyed

Discussion in 'Scripting' started by Snaipuha, Mar 13, 2023.

  1. Snaipuha

    Snaipuha

    Joined:
    Jul 4, 2017
    Posts:
    6
    I am making a system, where money gets collected and a cash particle flies to the money UI header, which then should increase. How would I detect that a particle has travelled to header or that it got destroyed? Solutions I tried but didn't work:
    1. Check remainingLifetime of particle (Due to different framerates, remainingLifetime never reaches 0 and the particle gets destroyed before that happens. Using some min threshold almost worked, but during lag I still missed some destroyed particles)
    2. Check particle distance to target (Same issue, particle gets destroyed too soon before I can compare distance)
    3. Compare last frame GetParticles() with current frame GetParticles(). (Somehow got the same result as with number 2)
    I have spent way too much time on this and still haven't figured out how to do this. Any help would be appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Usually one doesn't bother to "detect" this... one tells a tweener (such as ITween or Leantween or DOtween) that you want the coin to fly up there and get destroyed when it arrives.

    Tweeners are awesome: fire and forget.
     
  3. Snaipuha

    Snaipuha

    Joined:
    Jul 4, 2017
    Posts:
    6
    I used tweens before, but sometimes I needed to tween up to 300 objects and that was very bad for performance on mobile. When I changed to particle system, performance improved significantly but sadly sometimes the cash does not update properly.
     
  4. Deleted User

    Deleted User

    Guest

  5. Snaipuha

    Snaipuha

    Joined:
    Jul 4, 2017
    Posts:
    6
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    You don't want to be using the particles themselves as the source of truth for your money counter. You're spawning the particles yourself using Emit, I'm assuming? When you spawn the particles, make an estimate for the delay and update the counter based on the delay, not on the graphics. It will always be unreliable (framerate dependent) otherwise.
     
    Deleted User and Snaipuha like this.