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
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.