I want to make the NPC always Aim in the pitch rotation(up-down) to where the target is. When the NPC is rotated towards the right direction it works great. Like this:
But when i rotate the NPC 90 degrees, the aim is not rotating towards the pitch relative to where its facing.
I want my NPC to always aim relative to where its aiming the rifle.
here is the code:
local char = script.Parent
local charHumanoid = char.Humanoid
local anim = charHumanoid.Animator:LoadAnimation(script.Parent.M4.Aim)
local waist = char.UpperTorso.Waist
local waC0 = waist.Transform
function equipRifle()
char.M4.RifleWeld.Part1 = nil
char.M4.CFrame = char.RightHand.CFrame * CFrame.new(0,-0.6,-0.3) * CFrame.Angles(math.rad(-90),0,0)
char.M4.RifleWeld.Part1 = char.RightHand
anim:Play()
end
wait(2)
equipRifle()
while task.wait() do
local target = workspace.target
if target then
local x1,y1,z1 = char.HumanoidRootPart.CFrame:ToOrientation()
local x2,y2,z2 = char.LowerTorso.CFrame:ToOrientation()
local x3,y3,z3 = char.UpperTorso.CFrame:ToOrientation()
local direction = (target.Position - char.HumanoidRootPart.Position).unit
local verticalAngle = math.asin(direction.Y)
char.UpperTorso.Waist.C0 = waC0 * CFrame.Angles(-x2+x1,-y2+y1,-z2+z1) * CFrame.fromAxisAngle(-char.HumanoidRootPart.CFrame.LookVector,verticalAngle)
end
end


