How can I limit head rotations on an NPC looking at players?

So in my game when u talk to NPCs I make the NPCs look at the player.
Example: https://gyazo.com/60a0babb0e33463eab02592399e26fcd

As you can see in the example. The NPC can unrealistically turn his neck 360. How can I limit his head movements to be realistic? Ty for any help in advance!

My code:


			local neck = source.Head.Neck
			local NPC = source

			local cframe0 = neck.C0
			
			local function worldCFrameToC0ObjectSpace(motor6DJoint,worldCFrame)
				local part1CF = motor6DJoint.Part1.CFrame
				local c1Store = motor6DJoint.C1
				local c0Store = motor6DJoint.C0
				local relativeToPart1 =c0Store*c1Store:Inverse()*part1CF:Inverse()*worldCFrame*c1Store
				relativeToPart1 -= relativeToPart1.Position

				local goalC0CFrame = relativeToPart1+c0Store.Position--New orientation but keep old C0 joint position
				return goalC0CFrame
			end

			repeat
				runService.Heartbeat:Wait()
				
				local goalCF = CFrame.lookAt(NPC.Head.Position, char.Head.Position, NPC.UpperTorso.CFrame.UpVector)
				local setCF0 = worldCFrameToC0ObjectSpace(neck,goalCF)
				
				if endingHeadRotation == false then

					local tween = tweenService:Create(neck,TweenInfo.new(0.5),{C0 = setCF0})
					tween:Play()
				end
			until interaction.Value ~= source or dead

			endingHeadRotation = true

			local tween = tweenService:Create(neck,TweenInfo.new(0.5),{C0 = cframe0})
			tween:Play()

			task.wait(0.5)

			endingHeadRotation = false

try math.clamp to clamp the angle between two numbers

1 Like

How could I do that? I’m not too familiar with math.clamp.

math.clamp(min,max) returns a number between min and max.

You can calculate the angle the neck would have to turn and clamp it from turning past 90 degrees (or any number)

1 Like

And how can I get the angle of the neck?

I found this post with the same issue but no solution

I tried adding this

local goalCF = CFrame.lookAt(NPC.Head.Position, char.Head.Position, NPC.UpperTorso.CFrame.UpVector)
				local setCF0 = worldCFrameToC0ObjectSpace(neck,goalCF)
				
				local RelativeToHRP = NPC.HumanoidRootPart.CFrame:ToObjectSpace(char.Head.CFrame)
				local x,y,z = RelativeToHRP:ToOrientation()

				y = math.deg(y)
				print(y)
				if math.clamp(y, -140, 140) ~= y then
					print("OUT OF RANGE")
				end

It seemed to be inconsistent as it would print -140 or 140 at different angles.

I recommand use IKControl

1 Like

How can I use this to limit the head rotations? I am unfamiliar with IKControl

Set wight to 0,999 so the the npc gives up if it’s too hard to look.
Here’s video might help you IKControl youtube video

1 Like

I watched the video and tried this:

local ik = Instance.new("IKControl")
			ik.Type = Enum.IKControlType.LookAt
			ik.ChainRoot = NPC.UpperTorso
			ik.EndEffector = NPC.Head
			ik.Target = char.Head
			ik.Weight = 0.999
			ik.Parent = NPC:FindFirstChildOfClass("Humanoid")

however this makes the uppertorso move (which I do not want) and doesnt limit the heads movement.

Also causes janky movement like this: https://gyazo.com/68e20057f12e82d25df816d5b55c28cb