So I have this script using body gyro and stuff to make it so when a person gets within range of the NPC, the head turns to face it. When the player leaves the vicinity, the head turns back to normal. How would I make it so it limits the angle the head can turn to so when the player leaves the angle limit the head turns back to normal?
Code:
local head = script.Parent local bodyg = script.Parent.BodyGyro local bodyp = script.Parent.BodyPosition local studsAway = 15 bodyp.Position = head.CFrame.p while wait() do for i, v in pairs(game.Players:GetChildren()) do local character = game.Workspace:WaitForChild(v.Name) if character then local distance = (head.CFrame.p - character.PrimaryPart.CFrame.p).Magnitude if distance <= studsAway then bodyg.CFrame = CFrame.new(head.CFrame.p, character.PrimaryPart.CFrame.p) break else bodyg.CFrame = CFrame.new(head.CFrame.p, script.Parent.Parent.Parent.Look.CFrame.p) end end end end
Pictures of model:
https://gyazo.com/c0f5fd20bdb8f9949aaba121d24a21e0
https://gyazo.com/3ee6cfaff1a6d5d7279d2448dcf6c321
https://gyazo.com/e8e70a14357c555aab8348695935c950