[SOLVED] [PathFinding] Moving an NPC via Path, but crossing Water on a Boat

Hey there, DevForum members!

I’m having quite some trouble finding a solution to this problem that I’m having. Let me explain:

I have an NPC which moves around on a piece of land that is surrounded by water (basically an island). But when there’s nothing to do on the current island, it needs to cross the waters over to another island without swimming in it. For that I would want a boat to be spawned in, which will transport the NPC from one side to another.

I’m aware that such things like PathFindingLink exist, but setting them up manually is too much work as the map is humongous. So I would need some kind of system, which will make the NPC walk to the edge and then spawn a boat. The boat will continue the journey of the Path and stop at the edge of another island.

I hope that there is a solution to this problem. Any help will be very much appreciated! :white_heart:


Image showcase of 2 possible scenarios.

1 Like

its a perplexing notion, I think it just ends up being that whenever you detect that computepath == nopath, tell your npc to go to nearest edge (or city that can spawn boat), spawn boat, moveto() seat (or just manually assign humanoid to seat)

but even with a high cost, if theres no way around it without via water, then water will still get traversed right? so nopath probably won’t be declared. if this is the case you may have to end up putting barriers around the islands and use pathfindingmodifer object to prevent the npc from traversing over to force nopath computation. I’m sure theres a way to automate it in via script to generate barriers automatically but that may take some time to develop

2 Likes

It detects NoPath as cost of Water is math.huge. I have no idea how to detect the closest part (edge) before Water though.

1 Like

thats a more difficult issue,I don’t know how you would go about checking closest edge without predefined edges especially since an island can be composed of multiple parts of different sizes.

my only solution would be to raycast in cardinal directions (including diagonals), repeat these short raycasts & raycast down at the end of each to check if hit terrain water, whichever does hit first, that is the closest edge

I think the solution is when nopath is detected, you’ll want to recompute path, but do not include water cost. make the npc moveto through waypoints until you decide that a waypoint is in the water thus stop the npc immediately

I don’t know off my mind but you could check if waypoint has any information regarding swimming or being in water, otherwise you will need to check if the position of waypoint is very close to water

I have my own code that I use to check water but don’t want to go searching for it, this should work for you:

local materials, occupancies = game.Workspace.Terrain:ReadVoxels(Region3.new(position - Vector3.new(0, .5, 0), position + Vector3.new(0, .5, 0)), 4)

for i, material in pairs(materials) do

   if material == Enum.Material.Water then
        return true -- The position is in/near water
    else
       continue
    end

end

return false -- The position is not in/near water
    
 
1 Like

I have used your potential solution to think of a way for me to implement it into my game. I’ve came to a conclusion to using GetPartBoundsInRadius(), which looks like it has solved the problem. Thank you so much for your help!

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