BasicR15Raycast for IK

(sorry for my english)
i create Basic R15IKFootplant by using this Raycast method https://devforum.roblox.com/t/ikcontrol-footplant-and-strafing-for-rig-15/2221813
make it Enabled when Humanoid Not moving


local Debug = false
local Workspace = game:GetService("Workspace")
local Tween = game:GetService("TweenService")

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local Instancenew = Instance.new
local Vector3new = Vector3.new

local CFramenew = CFrame.new
local CFrameAngles = CFrame.Angles

local Color3fromRGB = Color3.fromRGB

local RaycastParameters = RaycastParams.new()
RaycastParameters.RespectCanCollide = true

local function Highlight(FillColor, FillTransparency, OutlineColor, OutlineTransparency, Instance)
	local Highlight = Instancenew("Highlight")

	Highlight.Adornee = Instance
	Highlight.FillColor = FillColor
	Highlight.FillTransparency = FillTransparency
	Highlight.OutlineColor = OutlineColor
	Highlight.OutlineTransparency = OutlineTransparency
	Highlight.Parent = Instance

	return Highlight
end

local function Part(Size, Parent, Name)
	local Part = Instancenew("Part")

	Part.Size = Size
	Part.CanCollide = false
	Part.Parent = Parent
	Part.Name = Name

	return Part
end

local LeftIKControl = Instancenew("IKControl")
local RightIKControl = Instancenew("IKControl")

local function LeftLegIKControl()
	LeftIKControl.EndEffector = Character:WaitForChild("LeftFoot")
	LeftIKControl.Target = Character:WaitForChild("LeftPartTarget")
	LeftIKControl.ChainRoot = Character:WaitForChild("LeftUpperLeg")
	LeftIKControl.Parent = Character:WaitForChild("Humanoid")

	if Humanoid.MoveDirection.Magnitude > 0 then
		local Stop = Tween:Create(RightIKControl, TweenInfo.new(0.5), {Weight = 0})
		Stop:Play()
		LeftIKControl.Enabled = false
	elseif Humanoid.MoveDirection.Magnitude == 0 then
		LeftIKControl.Enabled = true
		local L = Tween:Create(RightIKControl, TweenInfo.new(0.5), {Weight = 1})
		L:Play()
	end

	return LeftIKControl
end

local function RightLegIKControl()
	RightIKControl.EndEffector = Character:WaitForChild("RightFoot")
	RightIKControl.Target = Character:WaitForChild("RightPartTarget")
	RightIKControl.ChainRoot = Character:WaitForChild("RightUpperLeg")
	RightIKControl.Parent = Character:WaitForChild("Humanoid")

	if Humanoid.MoveDirection.Magnitude > 0 then
		local Stop = Tween:Create(RightIKControl, TweenInfo.new(0.5), {Weight = 0})
		Stop:Play()
		RightIKControl.Enabled = false
	elseif Humanoid.MoveDirection.Magnitude == 0 then
		RightIKControl.Enabled = true
		local R = Tween:Create(RightIKControl, TweenInfo.new(0.5), {Weight = 1})
		R:Play()
	end

	return RightIKControl
end

local function Weld(Part0, Part1, C0, C1)
	local Weld = Instancenew("Weld")

	Weld.Part0 = Part0
	Weld.Part1 = Part1
	Weld.C0 = C0 or CFramenew(0, 0, 0)
	Weld.C1 = C1 or CFramenew(0, 0, 0)
	Weld.Parent = Part1

	return Weld
end

local function Instances(Parent, ClassName, Name)
	for _, Instance in next, Parent:GetChildren() do
		if Instance:IsA(ClassName) and Instance.Name == Name then
			return Instance
		end
	end
end

local function Parent(Instance)
	return Instance and Instance.Parent
end

local function Raycast(RaycastOrigin, RaycastDirection)
	return Workspace:Raycast(RaycastOrigin, RaycastDirection, RaycastParameters)
end

local Clamp = math.clamp
local Radians = math.rad

local Size = Vector3new(0.5, 0.5, 0.5)
local White = Color3fromRGB(255, 255, 255)

local LocalPlayer = game:GetService("Players").LocalPlayer

local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Animate = Instances(Character, "LocalScript", "Animate")

if Animate then
	Animate.Enabled = true
end

local Humanoid = Character:FindFirstChildOfClass("Humanoid")

if Humanoid.RigType ~= Enum.HumanoidRigType.R15 then
	return print("Rig must be a R15 one!")
end

local HumanoidRootPart = Instances(Character, "BasePart", "HumanoidRootPart")
local HumanoidRootPartSize = HumanoidRootPart and HumanoidRootPart.Size

local HumanoidRootPartSizeX = HumanoidRootPart and HumanoidRootPartSize.Y

local HumanoidRootPartSizeY2 = HumanoidRootPart and HumanoidRootPartSize.Y / 2
local HumanoidRootPartSizeX2 = HumanoidRootPart and HumanoidRootPartSizeX / 2
local HumanoidRootPartSizeX4 = HumanoidRootPart and HumanoidRootPartSizeX / 4

local UpperTorso = Instances(Character, "BasePart", "UpperTorso")

local Waist = UpperTorso and Instances(UpperTorso, "JointInstance", "Waist")
local WaistC0 = Waist and Waist.C0


local RightUpperLeg = Instances(Character, "BasePart", "RightUpperLeg")
local RightLowerLeg = Instances(Character, "BasePart", "RightLowerLeg")
local RightFoot = Instances(Character, "BasePart", "RightFoot")

local RightLegSizeY = ( RightUpperLeg and RightUpperLeg.Size.Y or 0 ) + ( RightLowerLeg and RightLowerLeg.Size.Y or 0 ) + ( RightFoot and RightFoot.Size.Y or 0 )

local LeftUpperLeg = Instances(Character, "BasePart", "LeftUpperLeg")
local LeftLowerLeg = Instances(Character, "BasePart", "LeftLowerLeg")
local LeftFoot = Instances(Character, "BasePart", "LeftFoot")

local LeftLegSizeY = ( LeftUpperLeg and LeftUpperLeg.Size.Y or 0 ) + ( LeftLowerLeg and LeftLowerLeg.Size.Y or 0 ) + ( LeftFoot and LeftFoot.Size.Y or 0 )


local RightLegTarget = Part(Size, Character, "RightPartTarget")

local RightLegWeld = Weld(RightLegTarget, HumanoidRootPart, CFramenew(- HumanoidRootPartSizeX4, RightLegSizeY, 0))
local RightLegWeldC0 = RightLegWeld.C0

local LeftLegTarget = Part(Size, Character, "LeftPartTarget")

local LeftLegWeld = Weld(LeftLegTarget, HumanoidRootPart, CFramenew(HumanoidRootPartSizeX4, LeftLegSizeY, 0))
local LeftLegWeldC0 = LeftLegWeld.C0

if Debug then
	Highlight(Color3fromRGB(0, 0, 255), 0.5, White, 0.5, RightLegTarget)
	Highlight(Color3fromRGB(0, 255, 0), 0.5, White, 0.5, LeftLegTarget)
else
	RightLegTarget.Transparency = 1
	LeftLegTarget.Transparency = 1
end

local Runservice = game:GetService("RunService")

Runservice.Stepped:Connect(function()
	LeftLegIKControl()
	RightLegIKControl()
end)

Runservice.PreRender:Connect(function(DeltaTime)
	DeltaTime = Clamp(DeltaTime * 7.5, 0, 1)

	local HumanoidRootPartCFrame = HumanoidRootPart.CFrame
	local AssemblyLinearVelocity = HumanoidRootPart.AssemblyLinearVelocity

	local RightVector = AssemblyLinearVelocity * HumanoidRootPartCFrame.RightVector
	local LookVector = AssemblyLinearVelocity * HumanoidRootPartCFrame.LookVector

	--local Forward = Radians(LookVector.X + LookVector.Z)
	--local Sideways = Radians(RightVector.X + RightVector.Z)

	--Waist.C0 = Waist.C0:Lerp(CFrameAngles(- Forward, - Sideways, - Sideways) * WaistC0, DeltaTime)

	local RightLegRaycastResult = Raycast(( HumanoidRootPartCFrame * CFramenew(HumanoidRootPartSizeX4, - HumanoidRootPartSizeY2, 0) ).Position, Vector3new(0, - RightLegSizeY, 0))

	if RightLegRaycastResult then
		local NormalZ = RightLegRaycastResult.Normal.Z
		local LookVector = RightLegTarget.CFrame.LookVector * NormalZ

		RightLegWeld.C0 = RightLegWeld.C0:Lerp(CFramenew(0, RightLegRaycastResult.Distance + HumanoidRootPartSizeY2 - NormalZ - RightLegSizeY, 0) * CFrameAngles(LookVector.Z, 0, LookVector.X) * RightLegWeldC0, DeltaTime)
	else
		RightLegWeld.C0 = RightLegWeld.C0:Lerp(RightLegWeldC0, DeltaTime)
	end

	local LeftLegRaycastResult = Raycast(( HumanoidRootPartCFrame * CFramenew(- HumanoidRootPartSizeX4, - HumanoidRootPartSizeY2, 0) ).Position, Vector3new(0, - LeftLegSizeY, 0))

	if LeftLegRaycastResult then
		local NormalZ = LeftLegRaycastResult.Normal.Z
		local LookVector = LeftLegTarget.CFrame.LookVector * NormalZ

		LeftLegWeld.C0 = LeftLegWeld.C0:Lerp(CFramenew(0, LeftLegRaycastResult.Distance + HumanoidRootPartSizeY2 - NormalZ - LeftLegSizeY, 0) * CFrameAngles(LookVector.Z, 0, LookVector.X) * LeftLegWeldC0, DeltaTime)
	else
		LeftLegWeld.C0 = LeftLegWeld.C0:Lerp(LeftLegWeldC0, DeltaTime)
	end
end)

6 Likes

Please format your post properly beforehand, it’s simply horrible to read currently.

Edit: The author already fixed it, so this is not an active complaint anymore.

1 Like

can you please include a video or images

1 Like

…he gave it for free. What more could you possibly demand? This isn’t even constructive criticism nor feedback.

3 Likes

A readable post.
I was not judging the content he shared, but how it was presented. The price is not a relevant factor in this context, so I’m not sure where you’re coming from.
Besides, he already fixed it so it’s fine.