[SOLVED] Need Help On Tower Defense Enemy Movement System

So im making a Tower Defense Enemy Movement System and everything is finished, but while polishing I came around an issue for turning smoothly to the next Waypoint.
Basically I want to Smoothly transition to this CFrame:

CFrame.lookAt(workspace.Enemies[enemyID].HumanoidRootPart.Position, Vector3.new(nnoc.X, nnoc.Y, nnoc.Z))

(nnoc is the next waypoint’s CFrame)
And here is the full function for moving the enemy:

function Move_Enemy(enemy, enemyID)
	for x, node in pairs(workspace.Path.Nodes:GetChildren()) do
		local runTime = 0
		local alpha = 0
		local moveLerp
		local nextNode = workspace.Path.Nodes:FindFirstChild(tostring(x + 1))
		local previousNode = workspace.Path.Nodes:FindFirstChild(tostring(x - 1))
		if not previousNode then previousNode = workspace.Path.StartNode end

		local startCFrame = workspace.Enemies[tostring(enemyID)].PrimaryPart.CFrame
		local pathOffset = workspace.Enemies[enemyID]:GetAttribute("PathOffset")
		local nodeAttribute = workspace.Enemies[enemyID]:GetAttribute("Node")
		local pathZOffset = nil
		
		if node:GetAttribute("Direction") == "Left" then pathZOffset = -pathOffset else pathZOffset = pathOffset end
		
		local nodeOffsetCFrame = node.CFrame * CFrame.new(pathOffset, enemy.HipHeight, pathZOffset)
		local pnoc = previousNode.CFrame * CFrame.new(pathOffset, enemy.HipHeight, pathZOffset)
		
		local distance = (Vector3.new(nodeOffsetCFrame.X, nodeOffsetCFrame.Y, nodeOffsetCFrame.Z) - Vector3.new(pnoc.X, pnoc.Y, pnoc.Z)).Magnitude
		local lerpTime = distance / enemy.Speed

		moveLerp = RunService.Heartbeat:Connect(function(deltaTime)
			task.wait()
			runTime += deltaTime
			alpha = runTime / lerpTime

			workspace.Enemies[tostring(enemyID)]:PivotTo(startCFrame:Lerp(nodeOffsetCFrame, alpha))

			if alpha >= 1 then moveLerp:Disconnect() end
		end)
		
		repeat task.wait() until alpha >= 1
		if nextNode then
			local nnoc = nextNode.CFrame * CFrame.new(pathOffset, 1, pathZOffset)
			workspace.Enemies[enemyID]:PivotTo(CFrame.lookAt(workspace.Enemies[enemyID].HumanoidRootPart.Position, Vector3.new(nnoc.X, nnoc.Y, nnoc.Z)))
		else
			workspace.Enemies[enemyID]:Destroy()
		end
	end
	
end

At the moment, the turning is instant and snappy.
When I tried doing a Tween it was buggy and the Enemy stopped on the Waypoint and Teleported to where it should be afterwards.

Is there anyway to make a Tween run WHILE the enemy is being Lerped? If there is no way to use a tween in that case, what should I do to make a functioning Smooth Turn?

Thanks.

1 Like

Could you show a video maybe? also i recommend tweens, or if youre using humanoids, use walkToPart or walkToPoint.
It could be better just to take the easier path.

1 Like

You could try tweening the rotation slightly before the enemy reaches the waypoint giving a better effect of it turning?

1 Like

yeah, but the problem is that my tween acts weird when lerping AND it stops lerping until the tween is finished even though the function is being called in coroutine.wrap

1 Like

no humanoids for performance, but im going to try using Tween i guess.

1 Like

If you need any help just reply, im here to help.

1 Like

I did the same system using tweens, and the model’s parts are not following the HRP. The model is anchored and every parts are welded together. I tried setting the target CFrame to Pivot:To(CFrame) but it would instantly teleport to the next waypoint. Any suggestions?

1 Like

I was also wondering if Tweens are a great idea or not for a Tower Defense game, since I have checked other people making Tower Defense movement systems, and most of them use :Lerp and people also suggests it.

I just want the most optimized thing (and also the better choice) for a TD movement system.
@Dankinations

1 Like

It really depends, some games use lerp, some use tweens.

Tweens are more optimized because they do not need to run many times. lerp can make some performance issues so i recommend using tweens. Also with making it so when the enemy rotate, to do this you could go by checking when the movement tween ends.

It should be something like tween.Ended or somewhere like that, look in the docs for more info.

1 Like

Maybe the other parts in the model are also anchored? get only 1 part to be the “center” and make it anchored, weld it to all the other parts, and youre good with only tweening that!

this worked! But I discovered another issue using tweens; After like 50 enemies spawn, The other ones that are being spawned start glitching out and they like autodestroy themselves, and some of them even stay stuck in the beginning. Is there like a tween limit? Or is there a very easy fix for this?

yea, but only problem is that the Smooth Turn Tween is not running. The function is running and all that stuff but it looks like its impossible for an Object to tween while it is already being tweened. I don’t want the enemy to stop on the Node to turn.

Basically, I just never want the enemy to stop. If I add Tween.Completed:Wait() at the end of the Turn tween, the enemy stops to turn. But if I remove that line of code it just DOESN’T run.

Any errors in output? or something? i need a bit more information.

I solved everything, just forgot to update the topic.