Tower Defense | Enemies Desynchronization


I’m currently working on a tower defense game, but I’m having some problems with the enemies. The problem is related to updating the enemy’s position every 0.1 seconds. This interval allows an enemy that is at the corners to overshoot its waypoint, like this:

Right now, when an enemy changes their waypoint, I set the CFrame to the current waypoint. However, this approach leads to desynchronization issues because this cut is always different. A potential solution could be to account this cut by adding it to the direction of the next waypoint, as shown here:

But I’m struggling to implement this solution, if you have another way to fix this or anything that could help, is appreciated!

Here’s the code:

-- ...
local currentNode = nodes[enemyInfo.Node]
local nextNode = nodes[enemyInfo.Node + 1]

local direction = (nextNode.Position - currentNode.Position).Unit
local totalDistance = (nextNode.Position - currentNode.Position).Magnitude
enemyInfo.CFrame = + direction * distanceTraveled, nextNode.Position)

if distanceTraveled >= totalDistance then
	local cut = (nextNode.Position - enemyInfo.CFrame.Position)

	enemyInfo.Node += 1
	currentNode = nodes[enemyInfo.Node]

	enemyInfo.CFrame = currentNode.CFrame
	enemyInfo.SpawnTime = time()
-- ...