Improve Performance on Custom (Simple) AILerp Script

  • A* version: [5.2.4]
  • Unity version: [2021.3.45f2]

Hello, I’m currently using a heavily modified version of the AILerp script to move a large number of soldiers around in a simple way while keeping formation in 3 dimension (ie on a unity terrain using grid graph). Pathfinding is only done on the formation level (only a few dozen of these at once), so the lerp script is just for translating and rotating the individual objects (thousands of these), and that in a greatly simplified way. I also use an update manager calling update on each ailerp instance, rather than them all having their own Update(). For the translation/rotation math, I also do some minor things like breaking vectors out into individual parts like this:

nextPosition1.x = trPosition.x;
nextPosition1.y = trPosition.y;
nextPosition1.z = trPosition.z;

or using System.Object.ReferenceEquals, which net some small improvements.

There are a few fixed things in Unity though which seem to be hard caps on performance (unless you go a completely different route of using entities). Namely the get and set rotations (using local to be a bit faster):

tr.GetLocalPositionAndRotation(out trPosition, out trRotation);

tr.SetLocalPositionAndRotation(nextPosition1, trRotation);

and some of the calls needed to calculate rotation, lerping, height sampling and such:

terrain.SampleHeight(nextPosition)

targetRotation = Quaternion.LookRotation(viewDirection, Vector3.up);

trRotation = Quaternion.Lerp(trRotation, targetRotation, deltaTime * rotationSpeed);

As well as calculations for square roots and such. I’ve tried converting transform types and using burst to do the translating/rotating, but the overhead of the conversions more than consumes the benefit from the faster math, so to see an improvement there I think I’d have to use full on ECS.

Are there examples of more performant versions of AI Lerp? Or, is there some kind of somewhat simple way to convert the AI lerp script to ECS in the astarpathfinding project? One thing is converting the whole project to ECS is not really an option, and the individual soldier model/texture/animations are baked using GPU Instancer (another Unity asset). I see this:

is there an example or guide on converting an ailerp script to that? Keeping in mind my ailerp is very simple now, and not using much other than get position and rotation, math on translate/rotate and velocity, and set position and rotation. There’s no avoidance, collision, off mesh links, or individual pathfinding.

Thanks for any help.