R6 Leg Placement System

I’m trying to make an R6 IK-type leg placement system and I’m struggling to find a calculation that will correctly place the legs.

Right now I have invisible feet attached to the bottom of the player’s legs with a slight offset to ensure it hits the floor, which is working well with my footstep sound system, but my leg mover moves the legs too high at all times.

local function moveLegPosition(foot, touchPart)
	local chosenHip = torso:FindFirstChild(string.split(foot.Parent.Name, " ")[1] .. " Hip")

	if chosenHip and touchPart then
		local currentCFrame = chosenHip.C0
		local hipPos = currentCFrame.Position
		local hipRot = currentCFrame - hipPos

		local newY = hipPos.Y + touchPart.Size.Y
		local newPosition = Vector3.new(hipPos.X, newY, hipPos.Z)

		local newCFrame = CFrame.new(newPosition) * hipRot

		local tweenInfo = TweenInfo.new(0.05, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
		local tween = TS:Create(chosenHip, tweenInfo, {C0 = newCFrame})
		tween:Play()
	end
end

for _, foot in pairs(character:GetDescendants()) do
	if foot:IsA("BasePart") and foot.Name == "Foot" then
		foot.Touched:Connect(function(touchPart)
			if touchPart.Parent ~= character then
				print(foot.Parent.Name .. " foot touched " .. touchPart.Name)

				playFootstepSound(foot, touchPart.Material)
				moveLegPosition(foot, touchPart)
			end
		end)
	end
end
1 Like