Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Optimizing the movement of many gameobjects

Discussion in 'Editor & General Support' started by Zilby, Aug 14, 2017.

  1. Zilby

    Zilby

    Joined:
    Jan 25, 2017
    Posts:
    10
    So I'm making a game where the enemies are composed of several individual parts (ie: blocks). Each of these blocks can (and often do) have movement outside of the movement of the enemy. This movement is currently being executed in each block's FixedUpdate(). After looking at the profiler (post optimizing dynamic batching, implementing object pooling, and reducing the vertices count in my objects) I've determined that the main draw on my CPU is the massive number of calls to the enemy blocks' FixedUpdates.

    Specifically, the lines that are causing the most CPU usage (~26%) are these:
    Code (CSharp):
    1. if(moving)
    2.         {
    3.             if (reverse)
    4.             {
    5.                 transform.localPosition = Vector3.MoveTowards(transform.localPosition, start, speed * Time.deltaTime);
    6.                 if (transform.localPosition == start)
    7.                 {
    8.                     reverse = !reverse;
    9.                 }
    10.             }
    11.             else
    12.             {
    13.                 transform.localPosition = Vector3.MoveTowards(transform.localPosition, location, speed * Time.deltaTime);
    14.                 if (transform.localPosition == location)
    15.                 {
    16.                     reverse = !reverse;
    17.                 }
    18.             }
    19.         }
    What would be the best way to optimize this code for it to be run by over 1000 objects?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,198
    A couple of things:

    - Don't put a script on each thing. Instead, have one script that moves all of them. That'll save you some overhead when you're moving this many things.
    - Move all of those objects to the top of the hierarchy. Moving things that have parents are slightly more expensive.
    - Reduce the number of if-statements:
    - Instead of the moving-bool, have things that are not moving in their own list, which you don't do anything with.
    - Instead of having a reverse bool, change the value of your destination when you get to the current destination.

    All of these are absurd micro-optimizations that are only applicable to instances of moving thousands of things. Don't take this as general advice.

    If that's not enough to get acceptable performance, look into replacing the objects with instancing. That's harder code to write, but it's a system designed for exactly this scenario.
     
  3. Zilby

    Zilby

    Joined:
    Jan 25, 2017
    Posts:
    10
    Thanks! I'll go test those optimizations out to see if they make any drastic improvements. I did somewhat simplify the issue, thus some optimizations such as removing their parents or implementing instancing wouldn't work for my particular use case.

    I'm also looking into seeing if not moving them when they're outside the camera view will help as well, but I need to make sure the check is worth the payoff in terms of performance.
     
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    You already got a great answer, just 1 more thing.

    I'm not sure exactly how your game works, but you could also try to have some blocks getting refreshed less often than FixedUpdate. You can look into this thread for using coroutines efficiently. If you've got a thousand objects, my guess is that not all of them are visible to the player, and/or they are far enough away for the player not to notice that they're moving more jaggedly. But the main offender I see in your code is the "reverse" bool.
     
    Zilby likes this.
  5. Zilby

    Zilby

    Joined:
    Jan 25, 2017
    Posts:
    10
    Can confirm that after implementing a bunch of these changes (and some others) that my performance did increase significantly :]

    @DroidifyDevs, I considered both disabling movement past a distance or making it happen every other fixed update (or on a coroutine) but decided against it since occasionally my camera does zoom out and I didn't want everything to look low quality when that happens if it doesn't have to.
     
  6. LoveraSantiago

    LoveraSantiago

    Joined:
    Nov 20, 2017
    Posts:
    18