Forum rules - please read before posting.

Reducing CPU usage on Char.fixedUpdate

I have a lot of NPCs active in a relatively large city, like 50 or something :) I have noted that the updates are dragging down my cpu, see screenshot. I was wondering if there is any way to deal with this, I have tried hacking it a little so that people are turned off when not seen, and that updates are bailed out of say 4 out of 5 times. This does speed it up a lot, but I was just wondering if anyone else has any tips. Of course turning them off when they go offscreen means they dont actually move when you arent looking at them (dont blink!) but it does effectively reduce a lot of the cpu time. As you can see 58% is a lot, seems like its more in Unity5 maybe, but I could be dreaming as usual :) 

image

Comments

  • NPCs can cope with fewer components if you don't need them to do much.  You can get by without them having a Rigidbody, for example, which won't be a huge problem if they don't walk around a lot.
  • edited March 2015
    I thought I might as well post my optimisation here, it has speeded up my game significantly. The downside is the NPCs update very infrequently when off screen, you can choose how infrequently or never. It's a trade off, I actually want my NPCs to wander past me if I sit still thats why I left in some updating, but this is probably not my final solution, but for now its a good speed up.

    The first part is to turn something off when offscreen, I attach this script (RSL_TurnNPCoffWhenInvisible see link below)  to the object with the SkinnedMesh renderer (in my case a UMA person).


    Then, inside AC's NPC.cs  I adjust the top of FixedUpdate() to be like this 

    //my new variables
    public bool isOffScreen;// flagged when NPC is offscreen
    int toggleCounter;//used to keep track of how many updates we have "skipped"
    private void FixedUpdate ()
    {
    //crude hack, this means people will update only one in every 60 times when theyre not visible
    if (isOffScreen)
    {
    toggleCounter++;
    if (toggleCounter>60)//this means it will do an update only every x times
    {
    //WILL DO NORMAL UPDATE
    }
    else
    {
    //WILL NOT DO ANYTHING!.. 
    //Debug.Log("SKIPPED NPC UPDATE.");
    return;
    }
    }
    toggleCounter=0;
    //////

    //method continues as normal.. IE:
    if (activePath && followTarget)
    {
    FollowCheckDistance ();
    FollowCheckDistanceMax ();
    }
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.