NPC teleporting right before facing target

You can write your topic however you want, but you need to answer these questions:
I want this NPC to face the target before it attacks, the problem is that when I have the function to make the NPC face the target, before it attacks the NPC looks like he teleports towards the target. This bug can only be seen by the players though, watching the NPC chase from the server’s view is how I want it to look.

I have already tried making the function run after the :movetofinished.Wait() but if the NPC is next to the target and isn’t moving, the NPC wont face the target.

Down below is the function to make the NPC face the target before attacking, and the code that makes the NPC move towards the target is just a simple Humanoid:MoveTo() command


task.spawn(function()			

	local direction = Vector3.new(target.Position.X, hrp.Position.Y, target.Position.Z)

	for i = 0, 0.9, 0.05 do
		hrp.CFrame = hrp.CFrame:Lerp(CFrame.new(hrp.Position, direction), i)
		task.wait()
	end

end)

You don’t need to use Lerp, you can just use Roblox’s built in CFrame.lookAt

game:GetService("RunService").Heartbeat:Connect(function()
	workspace.NPC.PrimaryPart.CFrame = CFrame.lookAt(NPCroot.Position, CharacterRoot.Position * Vector3.new(1, 0, 1) + NPCroot.Position * Vector3.new(0, 1, 0))
end) 

This code is just to demonstrate how it’s done, you will need to incorporate this with your pathfinding system and make the NPC look at the closest target.

Just added it and looks like it works, but I still have the same problem where the NPC looks like he teleports to the target through the client, but watching it from the server looks fine

Here is a better video showing the NPC teleporting, but not while viewing through the server

I have just tried tweening the rotation and still the NPC teleports towards the player…

I finally found the solution… :man_facepalming:

Apparently if you rotate the NPC while it’s moving, it will teleporting through the client view, I just had it so that if the NPC isn’t moving it will rotate as so:

local hrp = NPC:WaitForChild("HumanoidRootPart")

if math.floor(hrp.Velocity.Magnitude) == 0 then
	hrp.CFrame = CFrame.lookAt(hrp.Position, target.Position * Vector3.new(1, 0, 1) + hrp.Position * Vector3.new(0, 1, 0))
end

Also the NPC will automatically face the target when using the MoveTo so there is no need to make him face the target before moving.

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