I’m using this tutorial on how to animate tools, and it works well. However, it doesn’t align properly with my first-person arms script. When I look down, the gun moves higher until its handle is sticking out, and when I look up, the gun phases into my hand.
Here’s what I mean:
I’ve tried editing the values of X by multiplying and adding to it, but it always seems slightly off, so I assumed there was a better way to do this that I wasn’t aware of.
Here’s my code:
local RunService = game:GetService('RunService')
local Player = game.Players.LocalPlayer
local Camera = workspace:WaitForChild('Camera')
local Character = script.Parent
local Humanoid = Character:WaitForChild('Humanoid')
local Ang = CFrame.Angles
local aSin = math.asin
local aTan = math.atan
local HeadHorFactor = 0
local HeadVertFactor = 1
local UpdateSpeed = 0.5
local Ang = CFrame.Angles
local aSin = math.asin
local aTan = math.atan
local Head = Character:WaitForChild('Head')
local LeftArm = Character:WaitForChild('Left Arm')
local RightArm = Character:WaitForChild('Right Arm')
local Humanoid = Character:WaitForChild('Humanoid')
local RootPart = Character:WaitForChild('HumanoidRootPart')
local Torso = Character:WaitForChild('Torso')
local Neck = Torso:WaitForChild('Neck')
local NeckOrgnC0 = Neck.C0
local RightShoulder = Torso:WaitForChild('Right Shoulder')
local RightShoulderOrgnC0 = RightShoulder.C1
local LeftShoulder = Torso:WaitForChild('Left Shoulder')
local LeftShoulderOrgnC0 = LeftShoulder.C1
local BodyAttach = Torso:WaitForChild("BodyAttach",5)
local rootJoint = Character.HumanoidRootPart.RootJoint
local function align(CameraCFrame, HeadHorFactor, HeadVertFactor, UpdateSpeed, MouseOriginPosition)
local Dist = (Head.CFrame.Position-CameraCFrame.Position).Magnitude
local Diff = Head.CFrame.Y-CameraCFrame.Y
local HeadPosition = Head.CFrame.Position
local TorsoLookVector = RootPart.CFrame.lookVector
local X = -(math.asin((MouseOriginPosition - CameraCFrame.Position).unit.y)) * -1
Neck.C0 = Neck.C0:Lerp(NeckOrgnC0*Ang(-(aSin(Diff/Dist)*HeadVertFactor), 0, -(((HeadPosition-MouseOriginPosition).Unit):Cross(TorsoLookVector)).Y*HeadHorFactor),UpdateSpeed/2)
local _, Y, Z = Neck.C0:ToEulerAnglesXYZ()
Neck.C0 = CFrame.new(Neck.C0.Position) * CFrame.Angles(X - 1.5, Y, Z)
local _, Y, Z = RightShoulder.C0:ToEulerAnglesXYZ()
RightShoulder.C0 = CFrame.new(RightShoulder.C0.Position) * CFrame.Angles(X, Y, Z)
local _, Y, Z = LeftShoulder.C0:ToEulerAnglesXYZ()
LeftShoulder.C0 = CFrame.new(LeftShoulder.C0.Position) * CFrame.Angles(X, Y, Z)
local _, Y, Z = BodyAttach.C0:ToEulerAnglesXYZ()
BodyAttach.C0 = CFrame.new(BodyAttach.C0.Position) * CFrame.Angles(X , Y, Z)
end
while task.wait(1/60) do
local MouseOriginPosition = Player:GetMouse().Origin.Position
if not Character.Downed.Value then
align(Camera.CoordinateFrame, HeadHorFactor, HeadVertFactor, UpdateSpeed, MouseOriginPosition)
end
end
Any help would be appreciated!