NPC Facing Player

Ok so I am making an AI script and I want npcs to stop around 5 studs before they touch the target (that works)

if closestTarget and closestTarget.Position and moving == false then
									-- Make the NPC face the target without vertical adjustments
									humanoidRootPart.CFrame = CFrame.new(
										humanoidRootPart.Position,
										Vector3.new(closestTarget.Position.X, humanoidRootPart.Position.Y, closestTarget.Position.Z)
									)
								end

However when this happens, if i stay within the 5 studs they dont move therefore i can move behind the npc and it wont change direction unless i move out of the radius

Above is a snippet of the code im using to rotate the npc to face me however it is quite poor as it teleports the root part to the position where in reality all i want to do is tween / smoothly move the HRP orientation to face the player

humanoidRootPart = NPC root
closestTarget = player root

Would appreciate a different method. i tried using a moving variable (when i stop moveTo i make it false) but it doesn’t really help…

1 Like

If you don’t want it to move then use cframe.lookAt(startpos, position to look at). It gives and orientation but keeps the original position

if closestTarget and closestTarget.Position and moving == false then
									-- Make the NPC face the target without vertical adjustments
									humanoidRootPart.CFrame = CFrame.lookAt(
										humanoidRootPart.Position, 
										Vector3.new(closestTarget.Position.X, humanoidRootPart.Position.Y, closestTarget.Position.Z)
									)
								end

Ive changed it to lookAt and dont get me wrong that was a pretty decent solution. instead of like teleporting to the player it does indeed only fix the orientation. however if this is done whilst the npc is moving then when it runs the look at it freezes in position to rotate. so its an inconvenient stutter

heres how im controlling the moving var

humanoid:MoveTo(pos)
					moving = true
					humanoid.MoveToFinished:Connect(function(reached)
						moving = false
					end)

any solutions??

I have a feeling the main reason is because the npc is still moving but i cant nail down a way of checking if its moving due to MoveDirection not working on npcs

Hello, sorry I’m late but I don’t exactly understand what you mean. Could you explain more?

Ok so like ive fixed it for the most part by managing the Velocity of the HRP of the npc. However id say my final issue is getting the setting to be smooth, take a look at this…

if closestTarget and closestTarget.Position and moving == false and math.floor(humanoidRootPart.Velocity.Magnitude) == 0 then
									humanoidRootPart.CFrame = CFrame.lookAt(
										humanoidRootPart.Position, 
										Vector3.new(closestTarget.Position.X, humanoidRootPart.Position.Y, closestTarget.Position.Z)
									)
								end

Now how exactly would i make the transition smooth because if i make a sudden jolt and walk around the npc it instantly turns around

Thanks!

Use :Lerp on the CFrame like this:

humanoidRootPart.CFrame = humanoidRootPart.CFrame:Lerp(CFrame.lookAt(
	humanoidRootPart.Position, 
	Vector3.new(closestTarget.Position.X, humanoidRootPart.Position.Y, closestTarget.Position.Z)
), 0.9) -- change this number to your liking (0 to 1)

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