I’m working on a 2d, realtime sprite-based roguelike. It has no collision so I can’t use a proper navmesh. In its place, when an NPC plans a route it compiles an array of Vector3 representing the points it needs to visit in order to reach its destination. Right now, every NPC runs this every fixedupdate:
void MoveTowards(Vector3 targetPos, float speed)
{
if (currentPosition == targetPos)
{
ArrivedAtWaypoint(); //Consult navgraph and get new starting and target positions
}
else
{
currentPosition = Vector3.Lerp(currentPosition , targetPos, Time.deltaTime * speed);
}
}
This works okay, but it’s rubberbandy and it can often be jerky (since the sprite is teleporting every frame). More importantly however, it feels extremely inefficient; under normal conditions there will be 20-30 NPCs in the game at any given time, and requiring each of them to run this in tick every single frame (even if most of the time it will just check against a bool to see if movement needs to be calculated) feels incredibly inefficient. Is there an obvious, more performant implementation I’m missing, or am I obsessing over optimizing something that’s already pretty fast?