I am trying to optimize my games performance and I noticed that one of my scripts is using 19% of the CPU Usage. This script is attached to all of my troops (50 - 100+ units) in an RTS Style game and is used for getting the correct number (“SpeedPercent”) for an Animation blend tree. When disabling all the troops with this script on them the FPS jumps up from ~15 FPS to ~50 FPS. I’m not sure what I can do improve this script or set the animator float more efficiently. Also, the editor loop is taking up a lot of CPU Usage (46.4%) but I’ve been told this is from running the profiler and will go away once the profiler is closed.
Below is the script.
public class CharacterAnimator : MonoBehaviour {
const float locomotionAnimationSmoothTime = .1f;
public float SpeedPercent;
public NavMeshAgent agent;
// Update is called once per frame
void Update () {
SpeedPercent = agent.velocity.magnitude / agent.speed;
animator.SetFloat ("SpeedPercent", SpeedPercent, locomotionAnimationSmoothTime, Time.deltaTime);
}
}