So, after doing some research I was able to come across a helpful guide by @LMH_Hutch on IK Footplanting, I was able to use it to create my script and I came fairly close to the end result I was trying to achieve but for some reason I can’t get the proper goal of where I want my foot to be placed instead it’s set to a constant that seems to be infront the Hip Joint instead of constantly changing the lookVector position when the player moves and it does this weird glitchy thing, here:
https://gyazo.com/5c156a88981c560aa303ab8bb6b99309
function IK:ReturnCos(L1, L2, L3)
local Angles = {}
Angles["1"] = -math.acos((-(L2 * L2) + (L1 * L1) + (L3 * L3)) / (2 * L1 * L3))
Angles["2"] = math.acos(((L2 * L2) - (L1 * L1) + (L3 * L3)) / (2 * L2 * L3))
return Angles
end
function IK:GetTargetPos(Frame)
local RayC = Ray.new(Frame.Position, (Frame.LookVector*5))
local _, Position = workspace:FindPartOnRay(RayC, self.Character)
--Debugging purposes
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Bright red")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (Frame.Position - Position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(Frame.Position, Position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 0.1)
return Position
end
function IK:Solve(OriginCF, TargetPos, L1, L2)
local Localized = OriginCF:pointToObjectSpace(TargetPos)
local LocalUnit = Localized.unit
local L3 = Localized.magnitude
local Axis = Vector3.new(0, 0, -1):Cross(LocalUnit)
local Angle = math.acos(-LocalUnit.Z)
local PlaneCF = OriginCF * CFrame.fromAxisAngle(Axis, Angle)
if L3 < math.max(L1, L2) - math.min(L1, L2) then
return PlaneCF * CFrame.new(0, 0, math.max(L1, L2) - math.min(L1, L2) - L3), -math.pi/2, math.pi
elseif L3 > L1 + L2 then
return PlaneCF, math.pi/2, 0
else
local A1 = self:ReturnCos(L1, L2, L3)["1"]
local A2 = self:ReturnCos(L1, L2, L3)["2"]
return PlaneCF, A1 + math.pi/2, A2 - A1
end
end
function IK:UpdateLeft()
local HipCF = self.LowerTorso.CFrame * self.HipLeftCache
local GoalP = self:GetTargetPos(self.LeftHip.C0)
local PlaneCF, HipA, KneeA = self:Solve(HipCF, GoalP, self.LeftUpperL, self.LeftLowerL)
self.LeftHip.C0 = self.LowerTorso.CFrame:toObjectSpace(PlaneCF) * CFrame.Angles(HipA, 0, 0)
self.LeftKnee.C0 = self.KneeLeftCache * CFrame.Angles(KneeA, 0, 0)
end
Anyone know how I could approach this problem and fix it?
EDIT:
After proof reading my post I think I have failed to explain well, ultimately what I’m trying to do is get the goal position relative to the HipJoint so when the player moves the goal position updates and so the Joints can adjust them selves to the position, something like this: