NPC stops moving when CFrame.lookAt() is used

Basically, I have a script that tells an NPC to run towards a player(or another NPC), while constantly turning towards them. Without the .lookAt(), the MoveTo() works fine, but with it, the MoveTo() is cancelled. Is there a way I can fix this?

game:GetService('RunService').Heartbeat:Connect(function()
	if script:GetAttribute('Target') ~= nil then
		local target = script:GetAttribute('Target')
		if target then
			if game.Workspace:FindFirstChild(target):FindFirstChild("HumanoidRootPart") then
				script.Parent.Humanoid:MoveTo((workspace:FindFirstChild(target).PrimaryPart.CFrame * CFrame.new(Vector3.new(0,0,-4))).Position)
				script.Parent.PrimaryPart.CFrame = CFrame.lookAt(script.Parent.PrimaryPart.Position,workspace:FindFirstChild(target).PrimaryPart.Position)
			end
		end
	end
end)

uuhhh probably because you added in script.Parent.PrimaryPart.CFrame = CFrame.lookAt(script.Parent.PrimaryPart.Position,workspace:FindFirstChild(target).PrimaryPart.Position) and it puts the NPC’s primary part to the position, and it works exactly the same as the deprecated function SetPrimaryPartCFrame() and I suggest to use script.Parent:MoveTo(CFrame.lookAt(script.Parent.PrimaryPart.Position,workspace:FindFirstChild(target).PrimaryPart.Position)) instead, but Comment the code if you don’t want it

I get the error Unable to cast CoordinateFrame to Vector3

I meant script.Parent:PivotTo(CFrame.lookAt(script.Parent.PrimaryPart.Position,workspace:FindFirstChild(target).PrimaryPart.Position)) I put that by mistake

Now he just stands in place, staring at the NPC but not moving

The issue with your current implementation is that the CFrame.lookAt() function changes the orientation of the NPC immediately to face towards the target, which cancels the MoveTo() command that was issued before. One way to solve this is to separate the rotation and movement commands, and update them in each frame separately. Here’s an example implementation that should work:

game:GetService('RunService').Heartbeat:Connect(function()
		if script:GetAttribute('Target') ~= nil then
			local target = script:GetAttribute('Target')
			if target then
				if game.Workspace:FindFirstChild(target):FindFirstChild("HumanoidRootPart") then
					local targetPos = workspace:FindFirstChild(target).PrimaryPart.Position
					local direction = (targetPos - script.Parent.PrimaryPart.Position).Unit
					script.Parent.PrimaryPart.CFrame = CFrame.lookAt(script.Parent.PrimaryPart.Position + direction, script.Parent.PrimaryPart.Position)
					script.Parent.Humanoid:MoveTo(targetPos - direction * 4)
				end
			end
		end
	end)

In this implementation, the direction vector between the NPC and the target is calculated, and the CFrame.lookAt() function is used to orient the NPC towards a point slightly in front of the target. Then, the MoveTo() function is used to move the NPC towards the target, but with a small offset to ensure that the NPC doesn’t immediately collide with the target. This approach should allow the NPC to move towards the target while constantly facing them.

You don’t need to do CFrame.lookAt . Humanoid’s will look at the position when asked.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.