Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Optimize moving particles in Update

Discussion in 'Scripting' started by FlyVC, May 23, 2024.

  1. FlyVC

    FlyVC

    Joined:
    Jan 5, 2020
    Posts:
    32
    In my game i have multiple planes where i move single particles(stars) to achieve a Starfield with parallax effect.
    Since its about 1k particles, it contributes about 80% of my update time of all my monobehaviours.
    What are ways to optimize this code?
    Would Unity Dots be a good case for a problem like this?

    Code (CSharp):
    1. private void FixedUpdate()  
    2. {    
    3.     for (int i = 0; i< MaxStars; i++)    
    4.     {
    5.         Vector3 pos = Stars[i].position + transform.position;
    6.         pos.y += -CalcedYShift;
    7.         if(pos.y < transform.position.y - yOffset)      
    8.         {
    9.             pos.y += FieldHeight; pos.x = Random.Range(0, FieldWidth) - xOffset;      
    10.         }
    11.         Stars[i].position = pos - transform.position;
    12.     }
    13.     Particles.SetParticles(Stars, Stars.Length);
    14. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    Can it just be a particle system? That's how most of the kool kids do their starfields. :)
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    There's also the Bethesda way of doing starfields. Not recommended! :D

    I hope you're not using a ParticleSystem each with one particle?

    Like Kurt mentioned, a single particle system that emits "star" particles on one side (vertical or horizontal) and moves them linearly with minimally varying speeds to the opposite side would be sufficient. It works with practically zero code even and it'll be fast even with 1000 particles.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    Actually I did make a custom starfield for my Jetpack Kurt, as I needed a specific easily-movable volume affected by varying winds, aware of FixedUpdate physics. It is purely to visualize "wind flow" right in front of the player, regardless of which direction they might look or turn their head during play.

    I didn't see a handy way to do that in the ParticleSystem, so this was my result:

    https://gist.github.com/kurtdekker/e3117bef40fbf329d87c2bfd3c0121d2

    The key takeaway of that script was to do as little work as possible in FixedUpdate().

    Here it is in action:

     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,683
    What's the complete code of the script? Is it just
    FixedUpdate
    with some fields?
     
  6. FlyVC

    FlyVC

    Joined:
    Jan 5, 2020
    Posts:
    32
    there was indeed no reason to do the star moving manually, the behavior update reduced from 6 to 1.5ms.

    The particle system itself is barely noticable with 0.15ms

    thanks! :)