I am currently writing my own, simpler version of a pathfinding system, which is based on raycasting (as Roblox’s default Pathfinding tends to be very inconsistent, and to elaborate, sometimes it worked as it should, sometimes it yielded NoPath…).
For debugging reasons, I am spawning bricks at the NPC’s current position (which I consider to be on the floor btw, since I later plan on drawing a raycast between that position and the destination’s position - this is being done in the destResult and torsoPos check), and the NPC does walk, however, the problem is that either the selected destination is incorrect, or the debug brick is spawning at the incorrect destination point.
Here is a video that could help visualise what is going on:
streamable. com/uufni4
(^ the regular link is converted to a download link here for some reason lmao)
And here is the path function of the script:
local function path()
task.wait()
if stuck.Value == false and debounce == false then
debounce = true
-- first pick the destination (Y has to be raycasted. if raycasted destination Y ends up higher than the raycasted torso Y then make the char jump)
local function part(pos, color)
local p = Instance.new("Part")
p.Position = pos
p.Size = Vector3.new(1,1,1)
p.Anchored = true
p.CanCollide = false
p.CanTouch = false
p.CanQuery = false
p.Parent = workspace
p.Color = color
game.Debris:AddItem(p, 3)
end
local torsoPos = Vector3.new(char.Torso.Position.X, char.Torso.Position.Y - 3, char.Torso.Position.Z)
part(torsoPos, Color3.fromRGB(0,255,0))
local rayOrigin = Vector3.new(char.Torso.Position.X + rollX(), char.Torso.Position.Y + 10, char.Torso.Position.Z + rollZ())
local rayDestination = Vector3.new(rayOrigin.X, rayOrigin.Y - 1000, rayOrigin.Z)
local destResult = workspace:Raycast(rayOrigin, rayDestination)
if destResult then
if destResult.Position.Y - torsoPos.Y > 3 then -- mb draw a raycast and find the intersection point so it jumps right before that point???
print("Character has to jump.")
elseif destResult.Position.Y - torsoPos.Y > 10 then
print("Destination is out of vertical reach, finding new path.")
path()
end
local destination = Vector3.new(rayOrigin.X, destResult.Position.Y, rayOrigin.Z)
char.Humanoid:MoveTo(destination)
char.Humanoid.MoveToFinished:Wait()
print(Vector3.new(math.round(char.Torso.Position.X),math.round(char.Torso.Position.Y),math.round(char.Torso.Position.Z)))
print(Vector3.new(math.round(destination.X),math.round(destination.Y),math.round(destination.Z)))
part(destResult.Position, Color3.fromRGB(255,0,0))
debounce = false
else
print("Finding new path...")
debounce = false
path()
end
else
print(char.Name .. " is stuck - path not created.")
if stuck.Value == false then
stuck.Value = true
end
end
end
For clarity:
RollX() and rollZ() are just separate math.random(-30,30) functions that decide where the character should move. The Stuck value is not being changed in the script yet, but it will be used for detecting if the character really can’t find any destination point. Although it would be a rare occasion, I will later make it so it takes three tries to find a path, and if none succeed, Stuck will be true. If true, the character will purely pick a completely random destination point (without raycasting) and repeatedly jump.
After the character finishes walking, I print the position of the character and the destination point’s position, and it seems to be roughly correct (in the video the printed Y values aren’t consistent, but this has been fixed and it’s ireelevant). There is a very insignificant deviation between X and Z, suggesting everything is correct, compared to the deviation seen with the debug bricks. However, I don’t know why the brick spawns somewhere else, and this becomes super enigmatic, as the destination point could be higher, but reachable by the character if I make them jump, however in such a case it is not clear if the character should jump, because the destination point could be somewhere else.
I bet this is a really small mistake and I’m not attentive enough to spot it…