I want to move the npc to look at the player but when animation is played the head is being affected by the animation since my idle animation has the head rotated slightly left.
llocal ha = script.Parent.Head.Neck
local aim = script.Parent.Humanoid.Animator:LoadAnimation(script.Aim)
script.Parent.Humanoid.Animator:LoadAnimation(script.idle):Play()
aim:Play()
aim:AdjustSpeed(0.01)
local ra = script.Parent.RightUpperArm.RightShoulder
local ha = script.Parent.Head.Neck
local raC0 = ra.C0
local haC0 = ha.C0
local function calculateVerticalAngle(vector1, vector2)
-- Normalize the vectors
local v1 = vector1.Unit
local v2 = vector2.Unit
-- Calculate the dot product
local dotProduct = v1:Dot(v2)
-- Calculate the angle in radians
local angleRadians = math.acos(dotProduct)
-- Convert the angle to degrees
local angleDegrees = math.deg(angleRadians)
return angleRadians
end
game:GetService("RunService").PreRender:Connect(function()
local ang =( script.Parent.HumanoidRootPart.Position.Y-workspace.targetB.Position.Y/math.abs(script.Parent.HumanoidRootPart.Position.Y-workspace.targetB.Position.Y)) *calculateVerticalAngle(script.Parent.HumanoidRootPart.CFrame.RightVector, workspace.targetB.Position-script.Parent.HumanoidRootPart.Position)
-- aim.TimePosition = script.Parent.Value.Value
local s = vector.angle(script.Parent.HumanoidRootPart.CFrame.RightVector* vector.create(1,0,1),script.Parent.HumanoidRootPart.CFrame.RightVector)
local s2 = vector.angle(script.Parent.HumanoidRootPart.CFrame.RightVector* vector.create(1,0,1),(workspace.targetB.Position - script.Parent.HumanoidRootPart.Position)* vector.create(1,0,1))
local vAng = -1*((script.Parent.HumanoidRootPart.Position.Y - workspace.targetB.Position.Y) / math.abs((script.Parent.HumanoidRootPart.Position.Y - workspace.targetB.Position.Y))) * vector.angle(workspace.targetB.Position-script.Parent.HumanoidRootPart.Position,(workspace.targetB.Position * vector.create(1,0,1) + vector.create(0,script.Parent.HumanoidRootPart.Position.Y,0))-script.Parent.HumanoidRootPart.Position)
--print(vertAngle)
ra.Transform = CFrame.identity
ha.Transform = CFrame.identity
ha.C0 = ha.C0:Lerp(haC0 * CFrame.Angles(0,math.clamp(s2-1.7,-3,2),0) * CFrame.Angles(math.clamp(vAng,-3,2),0,0),0.1)
ra.C0 = ra.C0:Lerp(raC0 * CFrame.Angles(0,math.clamp(s2-0.1,-3,2),0)*CFrame.Angles(0,0,0)* CFrame.Angles(0,0,math.clamp(vAng+0.75,-3,2)),0.1)
end)
This is how it works without animation(as it should be):
this is how it works with animation aims slightly off:
this is the animation: