NPCs: Physics or CFrame?

Hello.

I’m currently thinking what should I consider while making NPCs for my own game.

  • I plan to control a lot of NPCs in real time, with one script.
  • Most NPCs will use nodes to move from one point to another.
  • NPCs might stop and do random actions.
  • NPC will try to recalculate path when it happens to be pushed into an object
  • I want NPC to face the target.

It actually made me wonder… Should I trust Roblox’s physics (by using humanoid functions) or should I go with CFrame based NPCs still? It’s honestly a hard decision.

I worry about few cases when relying on physics:

  • NPC gets pushed into an object a bit too hard and gets stuck
  • Server latency might affect the knockback force.
  • Server might have too much handle.

It wouldn’t be bad if I would use collision groups, so there’s no risk of being randomly catapulted.
CFrame also does the job nicely. I can even rotate NPCs to face their target. I still need to do something about its interaction with terrain.


My attempts at making CFrame-based NPCs:

It’s your choice:

I’m not sure if Physics would handle animations better. And would you be tweening the CFrame?

IMO physics is always the way to go with moving humanoids. They do come with the issues of character’s getting stuck as you stated but you can use a combination of physics & CFrame to avoid that.

If a NPC can’t get to their target in a reasonable time, you’d assume they are stuck and you can CFrame them to where they should be.

Also I don’t think NPC’s using physics would be expensive compared to CFraming them.

I use lerp to get the current position. It’s pretty easy maths.
I grab the route length and get the speed of NPC.

When it comes to animations, I will use a simple script to handle it.

My CFrame stuff is pretty nice, but I still need to work on better terrain interactions and also obstacle detection.

Whatever I’m going for doesn’t require too much physics too.

CFrame is the way to go if you can do efficient obstacle/terrain detection.

Roblox’s internal physics replicates physics data to clients so this could be a problem when dealing with 50/100+ unanchored NPCs which could increase ping and ReceivedNetwork stats.

I’m working on a custom NPC system too, a NPC is shown as one transparent part for projectile reasons, it’s also anchored to prevent Roblox from doing unnecessary physics changes on the “hitbox” of the NPC. I’m also going to use CFrame physics to handle movement, which is gonna be tough when it comes to knockback…

My NPCs don’t use humanoids but instead uses attributes (such as WalkSpeed and Health) that are applied on the hitbox.
I don’t use humanoids because of this thread: https://devforum.roblox.com/t/client-side-npc-humanoid-replication-behavior/488773

P.S. It’s completely up to you if you want to use Humanoids!

1 Like