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:
- 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. - A new click will overwrite the first one. If the new click is not the floor, then stop.
- 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)