Why is this bug happening?

Hi i am making IK (inverse kinematics or whatever) but i have a bug when i set the legs to cancollide off it just bugs out? heres some footage:

yes i tried turning off can query doesnt work and when i do turn on cancollide this happens:

It just glides on the floor and the other leg ist even doing its job??

Heres the server script:

local runService = game:GetService("RunService")

local passenger = script.Parent
local root = passenger:FindFirstChild("HumanoidRootPart")
local hum = passenger:FindFirstChild("Humanoid")

local Target = game.Workspace:WaitForChild("Target")

local alignoreientation = root.AlignOrientation

--// iktargets

local iktargets = passenger:FindFirstChild("Targets")
local LeftTarget = iktargets:FindFirstChild("IKTarget_Left")
local RightTarget = iktargets:FindFirstChild("IKTarget_Right")

--// raycast parts
local raycastparts = passenger:FindFirstChild("RaycastParts")
local LeftRaycast = raycastparts:FindFirstChild("RaycastLeft")
local RightRaycast = raycastparts:FindFirstChild("RaycastRight")

--// IKcontrols
local LeftControl = hum:FindFirstChild("LeftLeg")
local RightControl = hum:FindFirstChild("RightLeg")

LeftControl.Pole = root.LeftLegAtt
RightControl.Pole = root.RightLegAtt

--// Module

local ProceduralModule = require(game:GetService("ReplicatedStorage").ProceduralModule)

--// RaycastParams
local rayCastParams = RaycastParams.new()
rayCastParams.FilterDescendantsInstances = {passenger}
rayCastParams.FilterType = Enum.RaycastFilterType.Exclude

coroutine.wrap(function()
	while task.wait(0.25) do
		hum:MoveTo(Target.Position)
	end
end)

runService.Heartbeat:Connect(function()
	local rayCast = workspace:Raycast(root.Position, -1000 * root.CFrame.UpVector, rayCastParams)
	
	if rayCast then
		local rotateToFloorCframe = ProceduralModule:getRotationBetween(root.CFrame.UpVector, rayCast.Normal)
		local floororientedCFRAME = rotateToFloorCframe * CFrame.new(root.Position)
		
		local dz = (root.Position.Z - Target.Position.Z)
		local dx = (root.Position.Z - Target.Position.X)
		local HorzizontalAngle = math.atan2(dx, dz)
		
		alignoreientation.CFrame = floororientedCFRAME.Rotation * CFrame.fromOrientation(0, HorzizontalAngle, 0)
		
	end
end)

while true do
	ProceduralModule:IkLegStep(LeftTarget, LeftRaycast, passenger.PrimaryPart, 2, 2, 1, 0.05, rayCastParams)
	task.wait(0.1)
	ProceduralModule:IkLegStep(RightTarget, RightRaycast, passenger.PrimaryPart, 2, 2, 1, 0.05, rayCastParams)
    task.wait(0.1)
end

Heres the module script:





local ProceduralModule = {}


--// Returns a CFrame rotation between two vectors
--// Credit: @EgoMoose (Roblox)
function ProceduralModule:getRotationBetween(u, v)
	local randomAxis = Vector3.new(1, 0, 0)
	
	local dot = u:Dot(v)
	if (dot > 0.99999) then
		return CFrame.new()
		
	elseif (dot < -0.99999) then
		return CFrame.fromAxisAngle(randomAxis, math.pi)
	end
	
	return CFrame.fromAxisAngle(u:Cross(v), math.acos(dot))
end



--[[

Parameters:

ikTarget ->			A reference to the IK target of a leg
ikRayPart ->		A reference to the raycast part of a leg
root ->				The root part of a model
stepDistance ->		A number that specifies a distance a model has to travel before this function executes
stepForward -> 		A number that specifies a length of a step
stepHeight -> 		A number that specifies a maximum height the leg reaches when stepping
stepWait -> 		A number that specifies a how long the step takes (should be a small value like 0.05)
rayCastParams -> 	A reference to the RaycastParams

--]]


--// Calculates the position of a leg
function ProceduralModule:IkLegStep(ikTarget: BasePart, ikRayPart: BasePart, root: BasePart, stepDistance: number, stepForward: number, stepHeight: number, stepWait: number, rayCastParams: RaycastParams)
	stepWait = stepWait or 0.05
	stepForward = stepForward or 0
	stepHeight = stepHeight or 2
	
	local rayCast = workspace:Raycast(ikRayPart.Position, Vector3.new(0, -1000, 0), rayCastParams)
	
	if rayCast then
		if not ikTarget:GetAttribute("PreviousPos") then
			ikTarget:SetAttribute("PreviousPos", Vector3.new(0, 0, 0))
		end
		
		local previousFootPos = ikTarget:GetAttribute("PreviousPos")
		
		local defaultFootPos = rayCast.Position
		local diff = (defaultFootPos - previousFootPos).Magnitude
		
		--// Do a step
		if diff > stepDistance then
			local finalFootPos = rayCast.Position + root.CFrame.LookVector * stepForward
			local middleFootPos = ((finalFootPos - previousFootPos) * 0.5 + previousFootPos) + Vector3.new(0, stepHeight, 0)
			
			coroutine.wrap(function()
				ikTarget.Position = middleFootPos
				task.wait(stepWait)
				ikTarget.Position = finalFootPos
				
				ikTarget:SetAttribute("PreviousPos", ikTarget.Position)
			end)()
		end
	end
end

return ProceduralModule

Module located in replicated storage

Server is located in the character model

So how can i fix this?

(also forget how the leg is going backwards i fixed that)

This seems to me to be a case where you didn’t set the humanoid hipheight to the correct height, go to the character humanoid and try changing the HipHeight to a higher value and press play to test (remember to leave all parts with CanCollide disabled except the HumanoidRootPart)

1 Like

should i set the legs canquery to off?

I think this too since you are using raycast, but test it

1 Like

Alright but also why does the other leg not work? I cant really figure it out?

The clipping issue been fixed? Im still reading the code

1 Like

This seems to me to be a problem in the leg model itself rather than in the script, try doing some print() to test if everything is going according to the script

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.