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

3 comments

0

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?

0

also it would be better if u used motor6ds instead of bodygyro the motor would pretty much automatically limit turning

0

how would I do that since it's a R6 character?