Multiple questions regarding AIPath and timeScale

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);

…to…

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:

  1. 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?

  2. 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?

  3. 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?

  4. AIPath is killing my framerate. What measures can I take to prevent this?

Thank you very much for your time.

I have other beginner-level questions to ask, but I’m hesitant to start a new thread and ask more questions if my first thread still hasn’t been replied to yet…

Hi

Generally if you want to use a high time scale, it is better to use AILerp as AIPath might overshoot at high timescales while AILerp will always follow the path exactly. AILerp also has much better performance.

Time.deltaTime already includes the time scale.

There was a similar question asked a short while ago: Why does AIPath not work with timescale? - #7 by aron_granberg

With that in mind, I’ve got a follow-up question…

My project uses Unity 4. Switching to Unity 5 would break a lot of things and require significant engineering time, so I’m very hesitant to do so. I understand that AILerp is only available in the latest version of your plugin, whereas I’m simply using the last Unity 4-compatible version.

Is there any chance you’d be willing to provide an AILerp script that is compatible with the Unity 4 version of the plugin?

I installed Unity 5, downloaded the latest version of the A* plugin, grabbed the AILerp script, put it into my Unity 4 project, added VectorMath to AstarMath so it would work, and told all of my game’s characters to begin using AILerp instead of AIPath.

The result is that my game’s characters twitch around and jerk forward at regular intervals: https://www.youtube.com/watch?v=A_pckOP3W8s It’s the same interval that their repath rate is set to.

How can I prevent my characters from twitching/jerking whenever they repath?

(Also, can I prevent a character from rotating ONLY on one axis? I don’t want my characters to tilt 45 degrees when they walk up stairs.)

Hi

I would suggest testing different modes for Seeker -> Start End Modifier -> Start Point to remove the jerking.
ClosestOnNode is probably the one you want.

Note sure if AILerp is the best movement script for that kind of characters though since AILerp’s movement is usually very rigid, not very fluid.