Face nodes while pathfinding

Hey all,

So basically what I am trying to do is make a pathfinding system that will move parts and angle them to the node while moving through the pathfinding sequence. Here is some example code I wrote up:

local pathfinding = game:GetService('PathfindingService')
local tws = game:GetService('TweenService')
local path = pathfinding:CreatePath()
local sp = workspace.StartPart
path:ComputeAsync(sp.Position, workspace.End.Position)
local points = path:GetWaypoints()

for i,v in pairs(points) do --// generate the visual nodes
    local r = Instance.new('Part')
    r.Shape = 'Ball'
    r.Anchored = true
    r.BrickColor = BrickColor.Blue()
    r.Transparency = 0.5
    r.Size = Vector3.new(1,1,1)
    r.Position = v.Position
    r.Parent = workspace
end

for i, m in pairs(points) do
    tws:Create(sp, TweenInfo.new(0.5), {CFrame = CFrame.new(m.Position)}):Play()
    wait(0.5)
end

Using this code yields this result:

https://gyazo.com/3ba9cba9e7326692ce6d143c39a3641d

However, what I am trying to achieve is something more along the lines of this:
image
As you can see, the part is actually facing the node.

Logically, I tried doing this to set the look vector like normal:

CFrame.new(m.Position, m.Position)

But when I do this, the part gets sent to a negative Y coordinate of -340282346638528859811704183484516925440

Does anyone have any advice or ideas?

I think you need to use CFrame:LookAt()

CFrame.lookAt() yields the same result as before, same thing just has a third argument that I don’t need. I tried setting a slight offset to one of the CFrames, but that still did not achieve the result I wanted.

CFrame.lookAt((CFrame.new(m.Position) * CFrame.new(0, 0, 0.0001)).p, m.Position, Vector3.new(0, 0, 1))

For future reference, I was able to achieve this by indexing the next node in the sequence for the second CFrame argument.

So:

for i, m in pairs(points) do
    tws:Create(sp, TweenInfo.new(0.5), {CFrame = CFrame.new(m.Position, points[i+1].Position)}):Play()
    wait(0.5)
end
1 Like