Make NPC face waypoints

  1. What do you want to achieve? Keep it simple and clear!
    I’m attempting to make an npc navigation system. The objects the npc is navigating to will not be moving yet it is very weird.

  2. What is the issue? Include screenshots / videos if possible!
    It’s facing the wrong way, and it seems to teleport instead of walk.

Here is my code:

function Shopper:Navigate(destination)
	self.Path:ComputeAsync(self.Char.PrimaryPart.Position, destination)
	local waypoints = self.Path:GetWaypoints()
	for i, waypoint: PathWaypoint in next, waypoints do
		if i < #waypoints then
			self:LookAt((waypoints[i + 1].Position + Vector3.new(0, 3, 0)) , 1, true)
		end
		self.Char.Humanoid:MoveTo(waypoint.Position)
		self.Char.Humanoid.MoveToFinished:Wait()
	end
end

and Shopper:LookAt

function Shopper:LookAt(destination, timer, yield)
	local d = Instance.new("Part", workspace)
	d.Size = Vector3.new(0.25, 0.25, 0.25)
	d.Anchored = true
	d.CFrame = getCFrame(self.Char.PrimaryPart.Position, destination)
	local h = Instance.new("Highlight", d)
	local t = TS:Create(self.Char.PrimaryPart, TweenInfo.new(timer or 0.5), {
		CFrame = getCFrame(self.Char.PrimaryPart.Position, destination)
	})
	t:Play()
	if yield then
		t.Completed:Wait()
	end
end

I saw this post related to the new, CFrame.fromMatrix, so I attempted to use that instead of CFrame.lookAt, but CFrame.lookAt doesn’t work properly as well.
the function getCFrame is just the code from that post.

local function getCFrame(position, lookAt)
    local lookVector = (position - lookAt).Unit 
    local modelUpVector = Vector3.new(0, 1, 0)
    local rightVector = lookVector:Cross(modelUpVector)
    local upVector = rightVector:Cross(lookVector)
    return CFrame.fromMatrix(position, rightVector, upVector)
end

I’ve tried inversing the vector3 in
self:LookAt((waypoints[i + 1].Position + Vector3.new(0, 3, 0)) , 1, true) by doing
-self:LookAt((waypoints[i + 1].Position + Vector3.new(0, 3, 0)) , 1, true)
Unfortunately that doesn’t work either. How am I supposed to properly orient the NPC to face the waypoint it’s walking to?

Another thing, when I comment out the code to make the npc look at the next waypoint, the choppy teleporting stops and it walks properly.
In addition, I’ve already set the network owner of the npc to nil (aka the server)

1 Like