How to rotate pathfinding model towards their next waypoint?

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)
Screenshot 2021-08-23 033919

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
2 Likes

You can use CFrame.lookAt which can be found here: CFrame | Roblox Creator Documentation

@keith_220, How do I use this, it’s a little bit confusing.

1 Like

Sorry you lost me their a little bit, how again do I use it?

It is the same as this.

30 Letters

Well yeah I know but how would I make it rotate toward the models next waypoint?

Instead of looking at the next waypoint position, why don’t you make it look at the finish direction. Like this.

start.CFrame = CFrame.new(start.Position, chosenRandomPosition.Position)

Or try this if that doesn’t work.

start.CFrame = CFrame.new(start.Position,waypoints[i].Position)
start.Position = waypoints[i].Position

(Edited)

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.

If start is a model then you have to use the `SetPrimaryPartCFrame method.

Well, you see start is actually a union.

Try this and let me know if it works:

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
2 Likes

It works! Thank you so much, I really appreciate it.

1 Like

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 :slight_smile:

No problem, thank you again for your help

1 Like

@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.

Hmm… I see. Do you have any other scripts that could modify chickens’ cframes? If no, then it might be the pathfinding function being strange.

Um, no.

Game > Workspace > Chicken > PathfindingScript

PathfindingScript:

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!

Well, the chickens actually make a path that goes into diff. places.

1 Like