Hello, I am wondering if this is a good zombie moving system for a tower defense game I might be creating in the future, and is it possible to recreate this system with half as much code and maybe cleaner? Right now, I am not using humanoids, as they are not necessary.
function Zombies:Move(zombie)
local uniqueSeed = Random.new()
task.spawn(function()
local hrp = zombie:FindFirstChild("HumanoidRootPart")
local ZOMBIE_OFFSET = uniqueSeed:NextNumber(-0.34, 0.34)
local Animator = zombie:WaitForChild("AnimationController"):WaitForChild("Animator")
local anim = Instance.new("Animation")
if zombie:GetAttribute("WalkAnimation") then
anim.AnimationId = "rbxassetid://14761002355"
else
anim.AnimationId = "rbxassetid://14755070010"
end
anim.Parent = Animator
local animation = Animator:LoadAnimation(anim)
animation.Looped = true
animation:Play()
if zombie:GetAttribute("WalkAnimSpeed") then
animation:AdjustSpeed(1 * zombie:GetAttribute("WalkSpeed") / zombie:GetAttribute("WalkAnimSpeed"))
else
animation:AdjustSpeed(1 * zombie:GetAttribute("WalkSpeed") / 4.7)
end
for waypoint = 1, #waypoints:GetChildren() do
local Distance = (hrp.Position - waypoints[waypoint].Position).Magnitude
local cframeLookat = CFrame.lookAt(hrp.Position, Vector3.new(waypoints[waypoint].Position.X, hrp.Position.Y, waypoints[waypoint].Position.Z))
local rx, ry, rz = cframeLookat:ToOrientation()
local dx, dy, dz = math.deg(rx), math.deg(ry), math.deg(rz)
local orientation = Vector3.new(dx, dy, dz)
local Time = GetTime(Distance, zombie:GetAttribute("WalkSpeed"))
local timeToNextWaypoint = (Time * 1.5) -- Increase for slower walking and decrease for faster
local moveTween = TweenService:Create(hrp, TweenInfo.new(timeToNextWaypoint, Enum.EasingStyle.Linear), {Position = Vector3.new(waypoints[waypoint].Position.X + (ZOMBIE_OFFSET*2.5), hrp.Position.Y, waypoints[waypoint].Position.Z + (ZOMBIE_OFFSET*2.5))})
moveTween:Play()
local turnSpeedFormula = (Time / Distance) * 1.8 -- Increase for slower turning and decrease for faster
local orientationTween = TweenService:Create(hrp, TweenInfo.new(turnSpeedFormula, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Orientation = orientation})
orientationTween:Play()
task.wait(timeToNextWaypoint - 0.015)
end
zombie:Destroy()
end)
end
Yeah, I’m doing this too, just to separate logical blocks of code. But you did this between each line, which don’t make sense. I would make it something like that
function Zombies:Move(zombie)
local uniqueSeed = Random.new()
task.spawn(function()
local hrp = zombie:FindFirstChild("HumanoidRootPart")
local ZOMBIE_OFFSET = uniqueSeed:NextNumber(-0.34, 0.34)
local Animator = zombie:WaitForChild("AnimationController"):WaitForChild("Animator")
local anim = Instance.new("Animation")
if zombie:GetAttribute("WalkAnimation") then
anim.AnimationId = "rbxassetid://14761002355"
else
anim.AnimationId = "rbxassetid://14755070010"
end
anim.Parent = Animator
local animation = Animator:LoadAnimation(anim)
animation.Looped = true
animation:Play()
if zombie:GetAttribute("WalkAnimSpeed") then
animation:AdjustSpeed(1 * zombie:GetAttribute("WalkSpeed") / zombie:GetAttribute("WalkAnimSpeed"))
else
animation:AdjustSpeed(1 * zombie:GetAttribute("WalkSpeed") / 4.7)
end
for waypoint = 1, #waypoints:GetChildren() do
local Distance = (hrp.Position - waypoints[waypoint].Position).Magnitude
local cframeLookat = CFrame.lookAt(hrp.Position, Vector3.new(waypoints[waypoint].Position.X, hrp.Position.Y, waypoints[waypoint].Position.Z))
local rx, ry, rz = cframeLookat:ToOrientation()
local dx, dy, dz = math.deg(rx), math.deg(ry), math.deg(rz)
local orientation = Vector3.new(dx, dy, dz)
local Time = GetTime(Distance, zombie:GetAttribute("WalkSpeed"))
local timeToNextWaypoint = (Time * 1.5) -- Increase for slower walking and decrease for faster
local moveTween = TweenService:Create(hrp, TweenInfo.new(timeToNextWaypoint, Enum.EasingStyle.Linear), {Position = Vector3.new(waypoints[waypoint].Position.X + (ZOMBIE_OFFSET*2.5), hrp.Position.Y, waypoints[waypoint].Position.Z + (ZOMBIE_OFFSET*2.5))})
moveTween:Play()
local turnSpeedFormula = (Time / Distance) * 1.8 -- Increase for slower turning and decrease for faster
local orientationTween = TweenService:Create(hrp, TweenInfo.new(turnSpeedFormula, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Orientation = orientation})
orientationTween:Play()
task.wait(timeToNextWaypoint - 0.015)
end
zombie:Destroy()
end)
end