Should be easy enough. You can play around with the code to your liking.
This is an example implementation. Place this inside of the character you want to move around the nodes. Make sure you have a folder in workspace with your nodes.
local pathfindingService = game:GetService("PathfindingService")
local nodesFolder = game.Workspace:WaitForChild("Nodes")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local function moveToNode(node)
local path = pathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentJumpHeight = 5,
AgentMaxSlope = 45,
})
path:ComputeAsync(character.PrimaryPart.Position, node.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Complete then
for _, waypoint in ipairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
warn("Pathfinding failed!")
end
end
local function moveToNodesSequentially()
for _, node in ipairs(nodesFolder:GetChildren()) do
if node:IsA("BasePart") then
moveToNode(node)
end
end
end
moveToNodesSequentially()
Unfortunately this falls out of my knowledge scope, but hopefully someone else has the solution. I am not super experienced in setting my own pre-defined nodes and restricting them like that.
You can implement your own version of A* pathfinding for this. There are many useful guides on how to do so online. This video is a great starting point in my opinion. The exact details on how A* should be implemented depend on the needs of your project.
To add onto what @player356377 said, to achieve the particular movement you showed in your pictures using A*, you can just set up the nodes and the edges such that each node has no diagonal neighbors. I edited your original image to show what the edges would be (in white lines)
thats the exact way i made mine before, (i did try to make node based path finding)
For example. if the NPC was in the top left, it would only be given two options:
Left or Down
but even with thys system, it would just move to random point, plus it had to reach each point then it made a decision, instead of deciding to go to another node and using the avalible nope path to go to it
What do you mean it would move to a random point? It would move diagonally or it would move to some non-node point?
Also, what do you mean it would decide after each node? That sounds like it might be a problem with how you implemented the actual traversing. I’ve never experienced an issue like that.
If it’s jumping to other nodes, then you have not set up the edges/neighbors correctly. If you’re using PathFindingService, then I can’t give much input on that since I haven’t really messed with it. I expect that you wouldn’t be able to get your desired behavior using it.
As far as I know, there’s no way tell that service that you want to use your own custom nodes. It uses some internal navmesh that it generates and I don’t know to what extent you can customize that.
i didnt use pathfindingsevices for my old system, heres an explanation of how it worked:
NPC targets a random node (first move)
NPC looks in a folder inside that node, in that folder are object values saying wher it can move next
NPC Puts those nodes it can move to into a table
NPC Chooses a random one
NPC Moves to that random node, then it clears that table and the cycle repeats
2 issues with this system:
NPC couldnt choose any node, then (using other nodes) pathfind to it
NPC would randomly go to nodes that it should not be able to, nor has in its table data
What I would do is use the nodes you have in the folder to generate a graph in code that represents the nodes and their neighbors. You can pass the graph into an A* solver (you’d need to implement this yourself or find an implementation online) and get a path back. From there, you just have the NPC traverse the path.
Doing this would address both issues you had with your old system:
Instead of choosing random nodes, the NPC is following an exact path so it knows which node it needs to walk to next.
Assuming you’ve set up nodes and the object values in the folder correctly, it should be impossible for a path to be calculated that involves moving to nodes that are not neighbors with other nodes.