Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make AI look at the player?

I've been testing my AI for a thief game. If he spots you, he'll look at you to investigate. But the AI's head doesn't move. How do I fix this??

My Code:

function lookAt(player)
	local is_in_front = NPC.PrimaryPart.CFrame:ToObjectSpace(player.Character.PrimaryPart.CFrame).Z < 0
	if is_in_front then
		NPC.Head.CFrame = CFrame.new(NPC.Head.CFrame.p,(NPC.Head.CFrame.p - player.Character.Head.CFrame.p).Unit)
	end
end

1 answer

Change this:

 NPC.Head.CFrame = CFrame.new(NPC.Head.CFrame.p-(NPC.Head.CFrame.p - player.Character.Head.CFrame.p).Unit) --// calling '.unit' isn't needed here since the 2nd part of the cframe constructor takes a world position not a direction as you have it

To either:

 NPC.Head.CFrame    = CFrame.new(NPC.Head.Position,player.Character.Head.Position)

--// OR

 NPC.Head.CFrame = CFrame.lookat(NPC.Head.Position,player.Character.Head.Position)

hope this helps! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.