Scripting NPC's

I was wondering, since NPCs are typically controlled server side, how do developers avoid choppy movement on the client? I’ve noticed some popular games have NPCs that move really smoothly even without a fixed path (like the NPCs used in fighting games), and I’m curious how they’re doing it.

The best way to achieve this is to let the server have authority over the clients. Upon beginning movement, the server fires an event to all clients. The server then keeps track of the current CFrame (either through tweening or CFrame lerping, the latter of which is preferred). Meanwhile, the client handles any visual changes.

As for NPC pathfinding, there are 3 methods to achieve “good” NPCs.
The first of which is to calculate the path on the server using PFS/your preferred pathfinding module. This is not recommended because this can skyrocket client network recv.

The second method is to calculate the path on BOTH the server and the client, then simply send the start position, end position, and PathParams to the client. This can lead to having a different path route on the server and client, so it is recommended to send the midpoint(s) of the path to the client (depending on the path’s length). Then, simply have the client path compute from start position to the next midpoint, then from this midpoint to the next midpoint/end position.

However, the best method is to use a hybrid combination of the second method above and a precalculated route. Instead of computing the path each movement, you instead have a set folder of possible path waypoints. If the NPC is simply roaming around, you use your own a* or etc. method to calculate the fastest route taking these preexisting waypoints into account. If the NPC is attacking/following a player or target, you use PFS/your pathfinder and compute the waypoints. This system allows for optimal execution speed, low client network recv, and allows you to customize where NPCs will roam in a fixed area, such as a city.
Take a city map for example. Instead of having the NPCs walk in the road, on the grass trails, etc, you can simply use a predefined route along the sidewalks and force all NPCs to use this route; only jumping into the road to follow a target. The only downside to this method is that you will need to take extra development time to build your own waypoints, which might be worth it depending on the game.

Hope this helps.

5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.