AI seems to run WAY slower on bigger maps

Hello! I am currently making a battle game that will include NPCs to fight alongside the players.
This game has been in development for about 4 months, and this is my first time encountering this issue.

It seems my AI tends to run slower (or completely stops in its tracks, and or starts stuttering (taking 1 step by 1)) on larger maps. I noticed this when I first imported my NPCs from the the creation place to the actual game, which as you can see, the creation place is a flat baseplate with really only small assets all over the place.

Now, when I imported it to the main game and tried the AI out here on the bigger / actual maps, AI runs MUCH slower. I will use this map for example (around 3k+ parts) which runs awfully slow

Correct me if i’m wrong, But what I assume is that due to the large part count and or all the obstacles in the map, the NPC has trouble calculation and or creating the path towards the player? And no, none of the NPCs have printed errors, and so far; i havent gotten a “pathfinding request too long” error either. Any help on how to reinforce or any knowledge I could use helps! Thank you.

1 Like

Yes, longer paths generally take longer to compute. Same with more complicated paths i.e. a lot of obstacles. So it makes sense that it runs slower on larger maps.

You’re not computing a new path for every step the AI takes, right?

Nope. Once the NPC finds an enemy, It targets the enemy until they have died and or are out of range of detection, So basically it just sticks with the first path it created; though it will create a new path if any new “obstacles” get in the way (vehicles, or other NPCs).

1 Like

Okay, doesn’t sound like there are any obvious inefficiencies, just a lot of stuff going on.

I don’t see how the npc can find an enemy target, without some sort of recalculation, unless the enemy target is always in a fixed position. Are you certain you are not recalculating other than if obstacles move in the way?

Oh, I completely read the other post wrong lol. But yes, the AI does recalculate the enemy position about every half second. And now that I think about it, That’s probably why the AI tends to just “stop” often. Is there another way I should possibly be doing the pathfinding?

Try reducing the frequency depending on the distance. Such as if you are 100 studs away, check every 5 seconds, if you are 10 studs away, every .5 secons, etc… Don’t use those actual numbers as you will probably want to create some sort of formula that works best for you, take the magnitude of the npc and the target, and use that distance to calculate the frequency of checks.

I will try this, Thank you! I’ll let you know how it turns out.

There sure are some tricks of optimization you can use in order to make it look as it were going great on and no AIs were stopping mid-run.

  1. Make these 0.5s larger or shorter depending on the distance the player has to the AI (as said by @Ejawesome2015). But maintain it always the same, by example, if the AI recalculated after 0.2s, make it recalculate again after 0.8s (0.5 + 0.5 = 0.2 + 0.8 = 1.0);
  2. Use voxel lighting if your game doesn’t require otherwise;
  3. Don’t have all your maps together in the same point of the Workspace;
  4. Put low level of detail on parts that dont require otherwise. 3K parts are a bunch to render, all with high quality, when you can’t even see them in a frenetic PvE battle;
  5. Anchor all non-moving objects;
  6. Make the in-game objects near to a player (.Magnitude) possible to collide with, and the further ones not. As well, you can change the colliding mesh of the parts;
  7. Keep objects transparent or non-transparent, but do not use partial transparency as much as possible. Also, deactivate the cast shadow property if not necessary.
1 Like

I’ll look into these and see if they make a difference as well, Much thanks!

1 Like

This was it! They run after enemies way smoother and with a lot less trouble it seems, I appreciate it!

It was no problem.
Glad it worked :slight_smile: