So, I’m making a spider, and the walk animations is weird. It won’t let me send a vid, so I’ll need to explain it. the back legs go first then the front legs. I want it to go: left front leg and back right leg, front right leg and back left leg. (I’m new)
local RunService = game:GetService('RunService')
local Model = script.Parent.Parent
local Torso = script.Parent
local MaxLeg = 3
local TweenService = game:GetService("TweenService")
local moving = false
local Legs = {Model["Front Right Leg"],Model["Back Right Leg"],Model["Front Left Leg"],Model["Back Left Leg"]}
local function Reach(CurrentJoint: BasePart, EndJoint:BasePart, Arm:BasePart, Joints, Leg)
local Distance = (EndJoint.Position - CurrentJoint.Position).Magnitude
local ArmLength = Arm.Size.Z
CurrentJoint.CFrame = CFrame.lookAt(CurrentJoint.Position, EndJoint.Position)
CurrentJoint.CFrame *= CFrame.new(-Vector3.zAxis * (Distance - ArmLength))
if CurrentJoint.Name == "1" or EndJoint.Name == "1" then
Joints[1].Position = script.Parent:FindFirstChild(Leg.Name).WorldCFrame.Position
end
Arm.CFrame = CFrame.lookAt(CurrentJoint.Position, EndJoint.Position)
Arm.CFrame *= CFrame.new(-Vector3.zAxis * (ArmLength/2))
Joints[1].Position = script.Parent:FindFirstChild(Leg.Name).WorldCFrame.Position
Torso.Position = Torso.Position
end
function RayCast(Origin: Vector3, Direction: Vector3, Ignore: {Instance})
local Raycast = workspace:Raycast(Origin, Direction, RaycastParams.new())
if Raycast then
if table.find(Ignore, Raycast.Instance) then
Raycast = RayCast(Raycast.Position, Direction, Ignore)
end
end
return Raycast
end
function GetNumber(Number: NumberValue)
local Table = {}
for _, leg in pairs(Legs) do
if leg:FindFirstChildWhichIsA("NumberValue").Value == Number then
if leg:FindFirstChildWhichIsA("NumberValue").Name == "Front" then
table.insert(Table, 1, leg)
else
table.insert(Table, 2, leg)
end
end
end
return Table
end
RunService.Heartbeat:Connect(function(deltaTime: number)
Torso.Position = Torso.Position + Vector3.new(-.1,0,0)
for _, Leg in pairs(Legs) do
local Joints = {}
local Arms = {}
local Oppisite = GetNumber(Leg:FindFirstChildWhichIsA("NumberValue").Value)
for _, joint in pairs(Leg.Joints:GetChildren()) do
Joints[tonumber(joint.Name)] = joint
end
for _, arm in pairs(Leg.Arms:GetChildren()) do
Arms[tonumber(arm.Name)] = arm
end
local Target = Leg.Target
local LastPos = Target.Position
local LastPos2 = Joints[1].Position
local MaxDistance = 7.5
local MaxDistance2 = 7.5
if Leg:FindFirstChildWhichIsA("NumberValue").Name == "Back" then
MaxDistance = 6
MaxDistance2 = 6
end
if (Target.Position - Torso:FindFirstChild(Leg.Name).Relax.WorldCFrame.Position).Magnitude > MaxDistance or (Target.Position - Model:FindFirstChild(Leg.Name).Joints["3"].Position).Magnitude > MaxDistance2 and moving == false then
spawn(function()
moving = true
local Cast = RayCast(Torso:FindFirstChild(Leg.Name).Relax.WorldCFrame.Position,Vector3.new(0,-15,0),Model:GetDescendants())
if Cast.Instance then
local Tween = TweenService:Create(Target,TweenInfo.new(.25),{Position = Cast.Position + Vector3.new(0,5,0)})
Tween:Play()
else
local Tween = TweenService:Create(Target,TweenInfo.new(.25),{Position = Torso:FindFirstChild(Leg.Name).Relax.WorldCFrame.Position + Vector3.new(0,-5,0)})
Tween:Play()
end
wait(.15)
if Cast.Instance then
TweenService:Create(Target,TweenInfo.new(.25),{Position = Cast.Position}):Play()
else
TweenService:Create(Target,TweenInfo.new(.25),{Torso:FindFirstChild(Leg.Name).Relax.WorldCFrame.Position - Vector3.new(0,-15,0)}):Play()
end
wait(.25)
spawn(function()
wait(.25)
moving = false
end)
end)
end
if (Target.Position - LastPos).Magnitude < .1 and (Joints[1].Position - LastPos2).Magnitude < .1 and (Joints[1].Position - Torso:FindFirstChild(Leg.Name).WorldCFrame.Position).Magnitude > 1 then return end
LastPos = Target.Position
LastPos2 = Joints[1].Position
local EndJoint = Joints[#Joints]
local RootJoint = Joints[1]
EndJoint.Position = Target.Position + Vector3.new(0,10,0)
for i = #Joints - 1, 1, -1 do
Reach(Joints[i], Joints[i + 1], Arms[i],Joints,Leg)
end
EndJoint.Position = Target.Position
for i = #Joints - 1, 1, -1 do
Reach(Joints[i], Joints[i + 1], Arms[i],Joints,Leg)
end
RootJoint.Position = script.Parent:FindFirstChild(Leg.Name).WorldCFrame.Position
Torso.Position = Torso.Position
for i = 2, #Joints do
Reach(Joints[i], Joints[i - 1], Arms[i - 1],Joints,Leg)
end
end
end)