Building a better pathfinder, character movement via mouse

UPDATE #2 (SOLVED): So I managed to solve the zigzag by using a second mouse function that would toggle a movement disabler. First click turns on the pathfinder, second turns it off.

UPDATE #1: the zigzag problem comes from clicking the mouse a second time while the first click is being executed. I’m not sure how to fix this.

I am building a movement script that will reduce movement to a single mouse click, and will eventually have the following rules:

  1. One mouse click will send the character to the location clicked.
    1a) Only a click on a floor will start movement. I want to be able to interact with other things.
  2. A new click will overwrite the first one. If the new click is not the floor, then stop.
  3. This should be compatible with tablet devices.

And that’s it. I want it to be simple. I’m starting with step 1. What I have works, for very short distances (see script). When it comes to long distances, it fails gloriously! (see video) I think I will have better luck with actual raycasting, as the mouse itself appears to hover in 3d space. Raycasting will give me a position on a part.

Has anyone else tackled this kind of movement? Should I head down a different path? It would be cool if I could create a custom nav mesh that had doorway waypoints, but I don’t see any options to manipulate this service…!

robloxapp-20210622-0947233|video

local PathfindingService = game:GetService("PathfindingService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
wait(1)
local human = player.Character.Humanoid
local hrp = player.Character:WaitForChild("HumanoidRootPart")
local path = PathfindingService:CreatePath()

mouse.Button1Down:Connect(function()

	path:ComputeAsync(hrp.Position,mouse.Hit.p)
	local waypoints = path:GetWaypoints()

	for i, waypoint in pairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			human:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		human:MoveTo(waypoint.Position)
		human.MoveToFinished:Wait(2)	
	end

end)

1 Like

Yeah PathindingService is super limited and can’t really be extended that easily :frowning:

You can definitely run your own pathfinding system tho, with algorithms like A* or its many variations and some kind of datastructure to represent the navigable areas. You can manually setup a graph where nodes are connected if they can be walked between in a straight line. Start path finding by navigating to the closest node or the closest node that has line of sight, perhaps using PathfindingService since it works OK for short distances, and same from the endpoint node to the actual desired destination.

Here’s an implementation of A* you can use if you want, I don’t have time to explain it right now but if you have questions ask away and I’ll help later :+1:

AStar.rbxmx (3.4 KB)

1 Like

I will check this out, thanks! Meanwhile, my code now accomplishes goal 1a and 2 and more. It updates the path while holding down the mouse button and stop when I release. It also identifies floors only, so I can activate the surroundings without moving.

Only 3 small problems with this code:

  1. Animation is a little jittery around the arbitrary distance that the waypoints refresh. I suspect I could play around with the walking speed and get this perfect.
  2. Some of my doors have hidden parts to keep people from passing through them. They are blocking raycasting, so I need to rethink their design.
  3. There is a very specific height that pathfinding thinks is walkable, but is only jumpable on my skinned mesh avatar. I need to tweak my settings somewhere to make it walkable.

If your implementation is even better, I will gladly chuck this one!

I ended up going a different route. Because I’m going to be mostly close quarters in my game, I opted to remove the pathfinding altogether, and focus on single button play and better response times. I made a little obstacle course for my character and it’s coming along nicely.