I have an issue where a humanoid that is playing an animation will create jank with forced headmovement.
The following animation is played:
When i run a server script that rotates the head towards the players face, the animation will override the headmovement before being corrected
The following script is run in
loop = RunService.Stepped:Connect(function() ... end)
local function GetVectors(cf1)
local sx, sy, sz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = cf1:GetComponents()
local X = math.atan2(-m12, m22)
local Y = math.asin(m02)
local Z = math.atan2(-m01, m00)
return Vector3.new(math.round(X*roundVal)/roundVal, math.round(Y*roundVal)/roundVal, math.round(Z*roundVal)/roundVal)
end
local function LookTo(lookatItem)
local goalCF
local success, response = pcall(function()
goalCF = CFrame.lookAt(head.Position, lookatItem.Position, torso.CFrame.UpVector)
end)
if not success then
goalCF = CFrame.lookAt(head.Position, lookatItem:GetBoundingBox().Position, torso.CFrame.UpVector)
end
local relativeToPart1 = Neck.C0*Neck.C1:Inverse()*Neck.Part1.CFrame:Inverse()*goalCF*Neck.C1
relativeToPart1 -= relativeToPart1.Position
local X, Y, Z = relativeToPart1:ToEulerAnglesXYZ()
X, Y, Z = math.deg(X), math.deg(Y), math.deg(Z)
--print(X)
local maxXAngle = 130
local minXAngle = -120
--X = math.clamp(X, minXAngle, maxXAngle)
if X < maxXAngle and X >= 0 then
X = maxXAngle
elseif X > minXAngle and X < 0 then
X = minXAngle
end
--print(X)
--print(relativeToPart1)
--print(CFrame.Angles(X,Y,Z))
local goalC0CFrame = relativeToPart1+Neck.C0.Position--New orientation but keep old C0 joint position
goalC0CFrame = CFrame.Angles(math.rad(X),math.rad(Y),math.rad(Z))+Neck.C0.Position
local vec, vec2 = GetVectors(goalC0CFrame), GetVectors(Neck.C0)
if vec ~= vec2 then
tween = TweenService:Create(Neck, tweeninfo, {C0 = goalC0CFrame}):Play()
end
end
Ive tried running the lookTo loop on a local script using renderstepped, but it became worse. Heartbeat didnt help either.
I tried renaming body parts to different names to cancel the animation while running the loop, but the animation wouldnt revert if ex. player goes out of range.
What i also tried to do is create a module that removes specific limbs from animations, which could work but is SUPER DEMANDING if using moonanimator.
Is there any way i can stop animating a specific limb on command while not lagging the game or ruining the NPC indefinently?