Rotating the NPC without messing with MoveTo?

So I am currently working on a system that involves NPCs for my game, the problem is that what I need to do is make the NPCs face the player, whilst moving to a certain location.

Now the movement and looking works fine, except when I use the function to make it look at the Player, the NPC begins to stutter, but still move forwards towards the given location.

After removing the Look at function I can confirm it was the problem, and I believe its cause its disrupting it some how and just making the NPC not walk as smoothly, so my question is simple, how can I fix this?

some code snippets

look at function

local module = {}

function module:EnemyFacePo(Model:Model, Position:Vector3)
	local CurrentPosition = Model.PrimaryPart.Position
	local LookAt = Vector3.new(Position.X, CurrentPosition.Y, Position.Z)
	
	Model.PrimaryPart.CFrame = CFrame.new(CurrentPosition, LookAt)
	
	return
end

return module

Enemy code for moving

EnemyFunctions:EnemyFacePo(Model, TargetRoot.Position)
Humanoid:MoveTo(TargetRoot.Position + OffsetPo)

Thanks!

2 Likes

Use CFrame.lookat() rather than CFrame.new()

CFrame.lookat() returns a CFrame with a position of the first vector3 but oriented towards the second vector3.

2 Likes

I tried this:

function module:EnemyFacePo(Model:Model, Position:Vector3)
	local CurrentPosition = Model.PrimaryPart.Position
	local LookAt = Vector3.new(Position.X, CurrentPosition.Y, Position.Z)
	
	Model.PrimaryPart.CFrame = CFrame.lookAt(CurrentPosition, LookAt)
	
	return
end

but the issue still persists, to be fair idk how to use lookAt so Im not sure If Im doing it correctly.

CFrame.new(Pos1, Pos2) and CFrame.lookAt(Pos1, Pos2) do basically the same thing, though. It’s purely a matter of preference.