Hey! My game uses IKControl to move arms to the cursor position, but sometimes arms snap to positions, making the game very hard. Any fix to this?
Relevant code snippets:
local function createIKControl(humanoid, chainRootName, endEffectorName, priority)
local char = humanoid.Parent
local ik = Instance.new("IKControl")
ik.Type = Enum.IKControlType.Position
ik.ChainRoot = char:FindFirstChild(chainRootName)
ik.EndEffector = char:FindFirstChild(endEffectorName)
-- Create a part to hold the attachment target
local targetPart = Instance.new("Part")
targetPart.Name = chainRootName .. "_IKTarget"
targetPart.Size = Vector3.new(0.1, 0.1, 0.1)
targetPart.Transparency = 1
targetPart.Anchored = true
targetPart.CanCollide = false
targetPart.CanQuery = false
targetPart.CanTouch = false
targetPart.Parent = workspace
-- Create and parent the attachment properly
local att = Instance.new("Attachment")
att.Name = chainRootName .. "_Target"
att.Parent = targetPart
ik.Target = att
ik.Weight = 10
ik.Priority = priority or 1 -- (Priority is always 10)
ik.SmoothTime = 0.1
ik.Enabled = false
ik.Parent = humanoid
return ik, targetPart
end
local function setupIK()
local char = player.Character
if not char then return end
local humanoid = char:FindFirstChildOfClass("Humanoid")
if not humanoid or humanoid.RigType ~= Enum.HumanoidRigType.R15 then return end
leftIK, leftIKTargetPart = createIKControl(humanoid, "LeftUpperArm", "LeftHand", 10)
rightIK, rightIKTargetPart = createIKControl(humanoid, "RightUpperArm", "RightHand", 10)
end
local function clampReach(pos, armLength)
local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
if not root then return pos end
local direction = (pos - root.Position)
if direction.Magnitude > armLength then
direction = direction.Unit * armLength
end
return root.Position + direction
end
local function getMousePositionOnPlane()
local character = player.Character
if not character then return Vector3.zero end
local root = character:FindFirstChild("HumanoidRootPart")
if not root then return Vector3.zero end
local pos = mouse.Hit.Position
return Vector3.new(pos.X, pos.Y, root.Position.Z)
end
RunService.RenderStepped:Connect(function()
local targetPos = getMousePositionOnPlane()
if leftIK and leftIKTargetPart then
if holdingM1 then
leftIK.Enabled = true
leftIKTargetPart.Position = clampReach(targetPos, maxArmReach)
else
leftIK.Enabled = false
end
end
if rightIK and rightIKTargetPart then
if holdingM2 then
rightIK.Enabled = true
rightIKTargetPart.Position = clampReach(targetPos, maxArmReach)
else
rightIK.Enabled = false
end
end
end)