Search Unity

Animator Set parameter hugely impacts performance

Discussion in 'Animation' started by UDN_46c91c6c-aeb1-471f-87fb-917fc1db02c9, Jul 2, 2019.

  1. UDN_46c91c6c-aeb1-471f-87fb-917fc1db02c9

    UDN_46c91c6c-aeb1-471f-87fb-917fc1db02c9

    Joined:
    Dec 3, 2017
    Posts:
    5
    I am using Animator as a state machine for the enemy behaviors. I tried adding about 20 enemies into the scene and I saw suddenly a frame drop to 40fps.

    I went crazy when I saw that the root of the problem was that the root of the problem was that "EnemyController" component was informing the animator of the enemy status. I have about ~10 parameters being updated on each frame. Reducing them by half boosted the fps to above 70. Leaving only one went to 120 fps and removing them all boosted it to almost 140.

    I suppose that animator is making a lot of work behind the scenes when a parameter is changed, but, certainly doesn't seem reasonable. I searched the API but I didn't find any information about why this happens and how to deal with it.

    Any hints about what's going on and how to address the problem?
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    506
    So to be clear, leaving the actual parameters in the controller but simply removing the script's animator.SetXXX calls gave you that performance increase?

    It could well be a bug in your Unity version so it might be worth trying a different one.

    Otherwise you might not be able to do much other than reducing the need to set those parameters. For example, you might keep track of the last value you set for each one and only let the Animator know when a value actually changes. It would be rather hacky, but that's hardly unusual when dealing with Animator Controllers.
     
  3. UDN_46c91c6c-aeb1-471f-87fb-917fc1db02c9

    UDN_46c91c6c-aeb1-471f-87fb-917fc1db02c9

    Joined:
    Dec 3, 2017
    Posts:
    5
    Exactly. Just by removing the Set calls, it works perfectly.
     
  4. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    584
    Have you tried using the pre-hashed parameter Set calls?

    You can pre-hash your parameter into an Id with Animator.StringToHash.

    That's the obvious solution as the string-based Set/Get calls do that internally every time you call them.
     
  5. UDN_46c91c6c-aeb1-471f-87fb-917fc1db02c9

    UDN_46c91c6c-aeb1-471f-87fb-917fc1db02c9

    Joined:
    Dec 3, 2017
    Posts:
    5
    Yes, I tried, little impact. I gained about 2-3 fps, so that's still not the problem.