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

3 comments
well cant u save the orientation of the head prior to entering the domain, and loop check if the head is still in the region, whilst in the same loop u might as well update the position of the head should it be in the radius?
also it would be better if u used motor6ds instead of bodygyro the motor would pretty much automatically limit turning
how would I do that since it's a R6 character?