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

CFrame help Making NPC's head face Player?

Asked by 6 years ago

I'm making it so that the NPC's head will face the nearest player, works fine. However If the NPC isn't facing perfectly forward then the angle is thrown off. I'm stuck at how to make sure the NPC will always face the player regardless of the base rotation of the head.

while wait() do
    local closest = nil
    local count = math.huge
    for i,v in pairs(game.Players:GetChildren()) do
        if v.Character then
            local dist = (v.Character.HumanoidRootPart.Position-script.Parent.HumanoidRootPart.Position).magnitude
            if dist < count and dist < 30 then
                closest = v.Character
                count = dist
            end
        end
    end
    if closest then
        local lookAt = CFrame.new(closest.Head.Position,script.Parent.Head.Position)
        script.Parent.Head.Neck.C1 = CFrame.new(0,-0.5,0) * CFrame.Angles(0,-lookAt.lookVector.X,0)
    end
end

The selection of the closest player works fine, it faces the character fine, just the rotation part.

This is with a perfect orientation, faces the player fine. This is when the base angle is different, as you can see the head is facing to the left of the player.

Thank you for taking the time to read!

0
What if you reset the head's orientation in your script? ax_gold 360 — 6y
0
Head's orientation will cause the player to die. NeonProfile 111 — 6y

Answer this question