I’m working on a simulation game in which the player frequently needs to speed up time. I currently have about 40 characters using an A* grid to pathfind around a very large environment (a multi-story building. Every floor is 60x60 meters).
When I increase the timeScale, the framerate drops sharply. I know that this is because the included AIPath script will recalculate it’s path every X seconds, so if I increase the time scale, the AIPath script will recalculate a character’s path more often.
I have attempted to resolve the problem by modifying the AIPath script so that every instance of Time.time is replaced with Time.realTimeSinceStartup. However, this has not caused a significant improvement in the game’s framerate when the timeScale has been increased.
(I acknowledge that increasing the timeScale causes physics simulations to occur more times per second. I have some physics objects in my scene, and I acknowledge that this may be the primary cause of the low framerate when the timeScale is increased.)
When the framerate is increased, the characters need to move faster to compensate for the new timeScale, so I have modified this line…
tr.Translate (dir*Time.deltaTime, Space.World);
tr.Translate (dir*Time.deltaTime * Time.timeScale, Space.World);
Characters now move faster, but only to a certain degree. It appears that once the timeScale is increased beyond a threshold (I have not found the exact number, somewhere between 2x and 10x) characters simply stop walking faster, as if their maximum movement speed is capped, no matter what the timeScale is.
Each character has a script that calculates the distance from its current position to its destination. This code is extremely simple:
Distance = Vector3.Distance(transform.position, Destination.position);
After modifying the AIPath script so that a character’s movement speed is multiplied by the current timeScale, the “Distance” variable now always reports an incorrect number. For example, a character who is standing right on top of their destination will report being 12 meters away from their destination, if timeScale has been increased to a large number like 10.
Additionally, I’d like to mention that AIPath is currently my game’s biggest resource hog: http://imgur.com/MGWajOu.png My framerate is 60 FPS when no characters are spawned, but 15 FPS as soon as I spawn 40 characters.
So, I suppose I have four inquiries:
To prevent AIPath from performing additional calculations when timeScale is increased, do I merely need to replace every instance of Time.time with Time.realTimeSinceStartup, or is there more to it than that?
When timeScale is increased, characters appear to move at the same speed they were moving at previously. I have attempted compensate for this by multiplying a character’s movement speed by timeScale, but characters’ movement speed appears to be capped nonetheless. How can I resolve this?
When a character’s movement speed is multiplied by timeScale, the Distance variable is reporting that they are a significant distance away from their destination even if they are right on top of it. How do I resolve this issue?
AIPath is killing my framerate. What measures can I take to prevent this?
Thank you very much for your time.