Hey, I was wondering how I could make these pathfinding models rotate toward their next waypoint. In stead of it looking like the models are moving forward facing their destination, instead they move to the left/right while moving forward.
RobloxAIclipfail.wmv (427.2 KB)
If you pay attention closely then you will notice that the chicken is moving sideways and forward. Instead of being rotated toward the destination and just going straight.
what I have(left) what I need(right)
Pathfinding/pickrandomfinish script
local tablecord = {game.Workspace.Finish1, game.Workspace.Finish2, game.Workspace.Finish3, game.Workspace.Finish4}
local chosenRandomPosition = tablecord[math.random(1, #tablecord)]
local start = script.Parent
local PathfindingService = game:GetService("PathfindingService")
while true do
local path = PathfindingService:CreatePath()
path:ComputeAsync(start.Position, chosenRandomPosition.Position)
local waypoints = path:GetWaypoints()
for i = 1, 1000 do
if i <= #waypoints then
start.Position = waypoints[i].Position
--DOES NOT WORK start.CFrame = CFrame.new(start.Position,waypoints[i].Position)
wait(0.1)
end
end
end
Well I already tried that and it does not work, and for some reason when I try to print chosenRandomPosition, it gives me “Finish1”, “Finish2”, “Finish3”, “Finish4”, and no start.CFrame = CFrame.new(start.Position, game.Workspace.chosenRandomPosition.Position) does not work either.
local tablecord = {game.Workspace.Finish1, game.Workspace.Finish2, game.Workspace.Finish3, game.Workspace.Finish4}
local chosenRandomPosition = tablecord[math.random(1, #tablecord)]
local start = script.Parent
local PathfindingService = game:GetService("PathfindingService")
while true do
local path = PathfindingService:CreatePath()
path:ComputeAsync(start.Position, chosenRandomPosition.Position)
local waypoints = path:GetWaypoints()
-- ipairs() loop is better (in my opinion)
for i, waypoint in ipairs(waypoints) do
-- 1. make the chicken look towards the next waypoint
start.CFrame = CFrame.new(start.Position, waypoint.Position)
-- 2. wait some time before moving
task.wait(0.1)
-- 3. make the chicken move to the next waypoint
start.Position = waypoint.Position
end
end
Thank you. Your problem was that your moved the chicken first, and then you made it look at the waypoint, which after movement already became its current position, so the CFrame rotate function didn’t work because you asked it to rotate the chicken so that it looks at its current position… Always make sure you do things in the right order
@debugger57, although they rotate towards their next waypoint, I have another problem: robloxapp-20210823-1431510.wmv (888.2 KB)
The chickens bounce around everywhere before finding their path.
local tablecord = {game.Workspace.Finish1, game.Workspace.Finish2, game.Workspace.Finish3, game.Workspace.Finish4}
local chosenRandomPosition = tablecord[math.random(1, #tablecord)]
local start = script.Parent
local PathfindingService = game:GetService("PathfindingService")
while true do
local path = PathfindingService:CreatePath()
path:ComputeAsync(start.Position, chosenRandomPosition.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in ipairs(waypoints) do
start.CFrame = CFrame.new(start.Position, waypoint.Position)
task.wait(0.1)
start.Position = waypoint.Position
end
end
Then it might be roblox pathfinding being strange. I dont really know how it works under the hood, sorry. One thing you can do is visualize your paths by doing a loop:
local tablecord = {game.Workspace.Finish1, game.Workspace.Finish2, game.Workspace.Finish3, game.Workspace.Finish4}
local chosenRandomPosition = tablecord[math.random(1, #tablecord)]
local start = script.Parent
local PathfindingService = game:GetService("PathfindingService")
while true do
local path = PathfindingService:CreatePath()
path:ComputeAsync(start.Position, chosenRandomPosition.Position)
local waypoints = path:GetWaypoints()
-- first, visualize with glowing neon ball parts
for i, waypoint in ipairs(waypoints) do
local visualizePart = Instance.new("Part")
visualizePart.Name = "PathfindingVisualizationPart"
visualizePart.Shape = Enum.PartType.Ball
visualizePart.Size = Vector3.new(2,2,2)
visualizePart.Anchored = true -- dont let them fall
visualizePart.BrickColor = BrickColor.White() -- tip: you can change this color to make each chicken have its unique path color, so its easier to distinguish them
visualizePart.Material = "Neon" -- make them glow
visualizePart.Position = waypoint.Position -- position the parts
visualizePart.Parent = workspace -- lastly, parent to workspace
end
-- and here we make the chicken move
for i, waypoint in ipairs(waypoints) do
start.CFrame = CFrame.new(start.Position, waypoint.Position)
task.wait(0.1)
start.Position = waypoint.Position
end
end
Try this and see if it is pathfinding being wrong and making chickens bounce. So we can see wheres the problem. Note: @ninjacookielegend forgot to make parts into balls, sorry, check it again. Also, I forgot about block type enum: its Enum.PartType, not Enum.BlockType. Sorry!