Alternatives to moving enemies in my tower defense game

I’m currently working on a tower defense game and I’m using tweenservice to move the enemies. However, whenever the enemies die the tween still continues until it reaches the next waypoint. Is there a better way to move the enemies?

Code:

function move(mobtomove,mobhumanoid,speed)
	local reachedend = false
	local node = path
	while reachedend == false do
		if mobtomove.Parent and mobhumanoid.Health > 0 then
			local nextnode = node:GetChildren()
			if (#nextnode > 0) then
				node = nextnode[1]
				local pnode = node.Parent
				mobtomove.PrimaryPart.Orientation = pnode.Orientation
				local movetween = tweenservice:Create(mobtomove.PrimaryPart,TweenInfo.new((pnode.Position-node.Position).Magnitude/speed,Enum.EasingStyle.Linear),{Position = node.Position})
				movetween:Play()
				movetween.Completed:Wait()
			else
				reachedend = true
			end
		else
			break
		end
	end
	if mobtomove.Parent and mobhumanoid.Health > 0 then
		base.CurrentHealth.Value -= mobhumanoid.Health
		warn(mobtomove.Name,"Has reached the end, mob health left:",mobhumanoid.Health,"base health left:",base.CurrentHealth.Value)
		mobtomove:Destroy()
	end
end

I believe you could use this, it has more than just the moving aspect but it should help.

If the mobs are rigs, and have animations etc, have you thought about using a pathfinding module?

SimplePath by V3N0M is really good for that.

If I use pathfinding and the enemy speed is too high they will just fly off the track which is why tween service is good but also bad. Same goes to @vf9r.

Why not just use collision groups to avoid this scenario? Or have you already tried this? I’m assuming by flying off the track you mean when the enemy attacks? I’m not really sure what you mean.

So a tower defense game consists of your towers and enemies trying to invade your base by walking along a set path. Your main objective is to use towers to kill your enemies. The reason im using tweenservice is because tweenservice makes it so if the enemy is going too fast they cant just fly off the course. If I use pathfinding service or humanoid:moveto and the humanoid is going superfast the movements will look messy as they will literally be flying around the map trying to reach the checkpoints.

So I get what you mean, I have attempted this in studio using pathfinding and this is what I got -

https://gyazo.com/01972ca36d174ece889650e8b71f0e18

I’m using normal pathfinding, but I applied a part onto the players head, and welded it to the player, this stopped the player from flying off the edge as they do with being scaled down.

image

It’s a bit hacky, but works well.

late, but you could use align position & align orientation, they aren’t bad, it gets a similar result as to tower battles.

2 Likes