How do I prevent NPCs from walking off an edge?

Hello again!

I have an NPC system, but NPCs can walk off edges, which then leads them into walking into the ocean; making them unable to get on land or swim away. (There is a ladder near a dock, but NPCs almost never use it.)

Somebody did suggest doing something with pathfinding, but I am not sure how I can incorporate it into the solution to my problem.

Heres a game with my NPC system, featuring Ariana from my game:

One solution is walling of the areas with transparent parts.

2 Likes

I want the players to still be able to walk off the edges if they want to. (There is a ladder on the dock, but almost 100% of the time, NPCs won’t go up the ladder.)

When you gets vector to move you can just do math clamp by all axes to position for nps dont go off some region, like this

local PositionToWalk = Vector3.new(10,10,5)
local MaxPosition = Vector3.new(5,5,5)
local ClampedPosition = Vector3.new(
math.clamp(PositionToWalk.X,-MaxPosition.X,MaxPosition.X),
math.clamp(PositionToWalk.Y,-MaxPosition.Y,MaxPosition.Y),
math.clamp(PositionToWalk.Z,-MaxPosition.Z,MaxPosition.Z)
)
print(ClampedPosition) -- Vector3.new(5,5,5)
1 Like

You can use collision groups its so easy just look at tbe developer hub thread of it

1 Like