Problems w/Inverse Kinematics

What I’m trying to get is for my first-person gun view models, with ik to make sure the arms are at the right place on the gun.

I’ve tried looking at the forums and there was nothing there. I’m completely dumbfounded.

Thanks in advance, Heres the code:

Gun Module:

local Gun = {}
Gun.__index = Gun


function Gun.new(viewmodel, gun)
	local character = viewmodel
	local solveModule = script.SolveIK
	local self = setmetatable({
		_workspace		= game:GetService("Workspace");
		_runService	= game:GetService("RunService");
		_gun = gun;
		_viewmodel = viewmodel;
		
		_upperTorso	= character:WaitForChild("HumanoidRootPart");
		
		_rightShoulder = character:WaitForChild("RightUpperArm"):WaitForChild("RightShoulder");
		_rightElbow	= character:WaitForChild("RightLowerArm"):WaitForChild("RightElbow");
		_rightWrist	= character:WaitForChild("RightHand"):WaitForChild("RightWrist");

		_leftShoulder = character:WaitForChild("LeftUpperArm"):WaitForChild("LeftShoulder");
		_leftElbow	= character:WaitForChild("LeftLowerArm"):WaitForChild("LeftElbow");
		_leftWrist	= character:WaitForChild("LeftHand"):WaitForChild("LeftWrist");

		
		_solveIK		= require(solveModule);
		
		----
		
		_RSHOULDER_C0_CACHE = nil;
		_RELBOW_C0_CACHE = nil;
		_LSHOULDER_C0_CACHE = nil;
		_LELBOW_C0_CACHE = nil;
		
		
		_UPPER_LENGTH = nil;
		_LOWER_LENGTH = nil;
		----


	}, Gun)
	self._RSHOULDER_C0_CACHE = self._rightShoulder.C0;
	self._RELBOW_C0_CACHE		= self._rightElbow.C0;
	self._LSHOULDER_C0_CACHE = self._leftShoulder.C0;
	self._LELBOW_C0_CACHE		= self._leftElbow.C0;

	
	
	self._UPPER_LENGTH			= math.abs(self._rightShoulder.C1.Y) + math.abs(self._rightElbow.C0.Y)
	self._LOWER_LENGTH			= math.abs(self._rightElbow.C1.Y) + math.abs(self._rightWrist.C0.Y) + math.abs(self._rightWrist.C1.Y)
	
	return self
end


function Gun:Fire()
	
end


function Gun:Reload()
	
end


function Gun:AimIn()
	
end


function Gun:AimOut()
	
end


function Gun:UpdateViewmodel()
	--self._viewmodel:SetPrimaryPartCFrame(workspace.CurrentCamera.CFrame)
	
	self._rightShoulder.Transform = CFrame.new()
	self._rightElbow.Transform = CFrame.new()
	self._rightWrist.Transform = CFrame.new()
	
	self._leftShoulder.Transform = CFrame.new()
	self._leftElbow.Transform = CFrame.new()
	self._leftWrist.Transform = CFrame.new()
	
	
	
	local shoulderCFrame = self._upperTorso.CFrame * self._RSHOULDER_C0_CACHE
	local goalPosition = self._gun.RHand.Position	
	local planeCF, shoulderAngle, elbowAngle = self._solveIK(shoulderCFrame, goalPosition, self._UPPER_LENGTH, self._LOWER_LENGTH)
	self._rightShoulder.C0 = self._upperTorso.CFrame:toObjectSpace(planeCF) * CFrame.Angles(shoulderAngle, 0, 0)
	self._rightElbow.C0 = self._RELBOW_C0_CACHE * CFrame.Angles(elbowAngle, 0, 0)
	
	
	shoulderCFrame = self._upperTorso.CFrame * self._LSHOULDER_C0_CACHE
	goalPosition = self._gun.LHand.Position	
	planeCF, shoulderAngle, elbowAngle = self._solveIK(shoulderCFrame, goalPosition, self._UPPER_LENGTH, self._LOWER_LENGTH)
	self._leftShoulder.C0 = self._upperTorso.CFrame:toObjectSpace(planeCF) * CFrame.Angles(shoulderAngle, 0, 0)
	self._leftElbow.C0 = self._LELBOW_C0_CACHE * CFrame.Angles(elbowAngle, 0, 0)

end


return Gun

Inverse kinematics module:

local function solveIK(originCF, targetPos, l1, l2)	
	
	local localized = originCF:pointToObjectSpace(targetPos)
	local localizedUnit = localized.unit
	local l3 = localized.magnitude
	
	
	local axis = Vector3.new(0, 0, -1):Cross(localizedUnit)
	local angle = math.acos(-localizedUnit.Z)
	local planeCF = originCF * CFrame.fromAxisAngle(axis, angle)
	
	if l3 < math.max(l2, l1) - math.min(l2, l1) then
		return planeCF * CFrame.new(0, 0,  math.max(l2, l1) - math.min(l2, l1) - l3), -math.pi/2, math.pi
		
		elseif l3 > l1 + l2 then
		return planeCF * CFrame.new(0, 0, l1 + l2 - l3), math.pi/2, 0
		
	else
		local a1 = -math.acos((-(l2 * l2) + (l1 * l1) + (l3 * l3)) / (2 * l1 * l3))
		local a2 = math.acos(((l2  * l2) - (l1 * l1) + (l3 * l3)) / (2 * l2 * l3))

		return planeCF, a1 + math.pi/2, a2 - a1
	end
end

----

return solveIK

Oops, forgot to show what is happening!
snip

What’s your question? Is there something specific about your solver that doesn’t work?

Also here’s a tutorial. 2 Joint 2 Limb Inverse Kinematics

I dont know whats going wrong, and i’ve already read that tutorial(thats how i made it)