Procedural animations like in phantom forces, using R6 IK

I’m trying to make procedural animations like in phantom forces, using R6 IK.
There’s too much delay when I’m walking, I did try to add Raycasting to make it work but no success.
I tried also making it with ai but Ai is too bad at scripting lol, it just doesnt work at all.

local R6IK = require(game.ReplicatedStorage.R6IK)
local plr = script.Parent

local chr

for _,characters in pairs(workspace:GetChildren()) do
	if characters:IsA("Model") and characters.Name == plr.Name then
		chr = characters
	end
end

local IKController = R6IK.New(chr)
local torsoSwing = 0
local side = false

local RlerpedFoot,LlerpedFoot = Vector3.new(), Vector3.new()

local LeftLegOffset = CFrame.new(-0.8, -3, 0)
local RightLegOffset = CFrame.new(0.8, -3, 0)

local torsoPosition =  chr:WaitForChild("Torso").CFrame
local rfplace, lfplace = (torsoPosition*RightLegOffset).Position, (torsoPosition*LeftLegOffset).Position

local function makeDebugPart(color)
	local p = Instance.new("Part")
	p.Shape = Enum.PartType.Ball
	p.Size = Vector3.new(0.3,0.3,0.3)
	p.Anchored = true
	p.CanCollide = false
	p.Material = Enum.Material.Neon
	p.Color = color
	p.Parent = workspace
	return p
end

local leftDebug = makeDebugPart(Color3.fromRGB(0,255,0))
local rightDebug = makeDebugPart(Color3.fromRGB(255,0,0))
local leftFinal = makeDebugPart(Color3.fromRGB(0,150,255))
local rightFinal = makeDebugPart(Color3.fromRGB(255,150,0))

local function getFootTarget(originCFrame, offset)
	local origin = originCFrame.Position + Vector3.new(0, 2, 0)
	local direction = (originCFrame.LookVector * offset.forward) + Vector3.new(0, -offset.down, 0)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {chr, leftDebug, rightDebug, leftFinal, rightFinal}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude

	local rayResult = workspace:Raycast(origin, direction, raycastParams)
	if rayResult then
		return rayResult.Position
	else
		return origin + direction
	end
end

local forwardStep = 0
local downStep = 5

game:GetService("RunService").Heartbeat:Connect(function(dt)
	local torsoPosition =  chr:WaitForChild("Torso").CFrame

	torsoSwing = math.sin(tick()*chr.Humanoid.WalkSpeed)

	if torsoSwing > 0 and side then
		side = not side
		lfplace = getFootTarget(torsoPosition * LeftLegOffset, {forward = forwardStep, down = downStep})
	end

	if torsoSwing < 0 and not side then
		side = not side
		rfplace = getFootTarget(torsoPosition * RightLegOffset, {forward = forwardStep, down = downStep})
	end

	RlerpedFoot = RlerpedFoot:Lerp(rfplace, math.min(1/0.075*dt, 2)) -- /2
	LlerpedFoot = LlerpedFoot:Lerp(lfplace, math.min(1/0.075*dt, 2)) -- /2

	local raise = math.sin((RlerpedFoot - rfplace).magnitude/2)
	local raise2 = math.sin((LlerpedFoot - lfplace).magnitude/2)

	local finalLeft = LlerpedFoot + Vector3.new(0, (raise2/2), 0)
	local finalRight = RlerpedFoot + Vector3.new(0, (raise/2), 0)

	IKController:LegIK("Left", finalLeft)
	IKController:LegIK("Right", finalRight)

	leftDebug.Position = LlerpedFoot
	rightDebug.Position = RlerpedFoot
	leftFinal.Position = finalLeft
	rightFinal.Position = finalRight
end)

same with sideways.

1 Like