Part spinning instead of facing

Hi so i am having a problem with a rotation script, Instead of the foot facing where the humanoid root part is facing the part is just spinning around

Heres the spinning script:

hum.Running:Connect(function()
	ProceduralModule:IkLegStep(left_IkTarget, left_RaycastPart, passenger.PrimaryPart, 0.5, 2, 1, 0.05, rayCastParams)
	left_IkTarget.Orientation += Vector3.new(0, root.Orientation.Y, 0)
	task.wait(0.1)

	ProceduralModule:IkLegStep(right_IkTarget, Right_RaycastPart, passenger.PrimaryPart, 0.5, 2, 1, 0.05, rayCastParams)
	ikTargets.Parent.Legs.footR.Orientation += Vector3.new(0, root.Parent.Torso.Orientation.Y, 0)
	task.wait(0.1)
end)

Heres the entire script:

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

local runService = game:GetService("RunService")

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

local target = workspace:FindFirstChild("Target")

--// IkTargets
local ikTargets = passenger:FindFirstChild("IkTargets")
local left_IkTarget = ikTargets:FindFirstChild("LeftTarget")
local right_IkTarget = ikTargets:FindFirstChild("RightTarget")

--// Raycast parts
local raycastParts = passenger:FindFirstChild("RaycastParts")
local left_RaycastPart = raycastParts:FindFirstChild("LeftRaycast")
local Right_RaycastPart = raycastParts:FindFirstChild("RightRaycast")


--// IKControls
local left_IKControl = hum:FindFirstChild("LeftLegIK")
local right_IKControl = hum:FindFirstChild("RightLegIK")

left_IKControl.Pole = root.LeftAtt
right_IKControl.Pole = root.RightAtt

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



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.X - target.Position.X)
		local horizontalAngle = math.atan2(dx, dz)

		alignOrientation.CFrame = floorOrientedCFrame.Rotation * CFrame.fromOrientation(0, horizontalAngle, 0)
	end
end)

hum.Running:Connect(function()
	ProceduralModule:IkLegStep(left_IkTarget, left_RaycastPart, passenger.PrimaryPart, 0.5, 2, 1, 0.05, rayCastParams)
	left_IkTarget.Orientation += Vector3.new(0, root.Orientation.Y, 0)
	task.wait(0.1)

	ProceduralModule:IkLegStep(right_IkTarget, Right_RaycastPart, passenger.PrimaryPart, 0.5, 2, 1, 0.05, rayCastParams)
	ikTargets.Parent.Legs.footR.Orientation += Vector3.new(0, root.Parent.Torso.Orientation.Y, 0)
	task.wait(0.1)
end)

Heres the module script:

-- Author: Roox4



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

So why ist it working?

Can you show us a video of the issue? It is hard to understand it like this.

1 Like

Alright

here

robloxapp-20241213-1139125.wmv (1.2 MB)

Do you see how the foot is like spinning around its not facing the humanoid root part?

Sorry for delayed reply, I didn’t see that you replied.
Anyways, won’t doing this work?

hum.Running:Connect(function()
	ProceduralModule:IkLegStep(left_IkTarget, left_RaycastPart, passenger.PrimaryPart, 0.5, 2, 1, 0.05, rayCastParams)
	left_IkTarget.Orientation += Vector3.new(0, root.Orientation.Y, 0)
	ikTargets.Parent.Legs.footL.Orientation += Vector3.new(0, root.Parent.Torso.Orientation.Y, 0)
	task.wait(0.1)

	ProceduralModule:IkLegStep(right_IkTarget, Right_RaycastPart, passenger.PrimaryPart, 0.5, 2, 1, 0.05, rayCastParams)
	ikTargets.Parent.Legs.footR.Orientation += Vector3.new(0, root.Parent.Torso.Orientation.Y, 0)
	task.wait(0.1)
end)
1 Like