Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make AI look at the player?

Asked by 3 years ago

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

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago

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! :)

Ad

Answer this question