Lerp in Enemy System Help

I am creating an enemy system in my tower defense game. I decided to use ‘lerp’ to calculate the movement as it does cause CPU leaks. But the only problem is, how do I calculate the alpha in Lerp?

I tried a lot of ways but it just ended up failing. I really need help on the math to find alpha.

5 Likes

It depends. Is your path linear?

3 Likes

yes, mostly going to be the same speed

2 Likes

In that case, alpha is the time from the starting position to the ending.

local t = 0
while t < 1 do
local position = startingPosition:lerp(endingPosition, t)
t+= task.wait()
end
2 Likes

I appreciate the help, but this will cause a lot of lag when running 1000 enemies. I am looking for an equation that changes alpha every time after the enemy moves, so it looks like movement. I run a heartbeat event to loop through all the enemies and move them

2 Likes

Like a transition kinda to replicate movement, and also somehow i need to make speed take affect on this to

2 Likes

In that case, you should use time() to get the current game time. When you begin to run your enemies, you should apply some sort of time offset to when the enemy started.

--enemy.start is when it started moving
--enemy.end is when the movement ends
--enemy.startPosition
--enemy.endPosition
--enemy.position is the position

--in your heartbeat function, a for loop that iterates through all the enemies
enemy.position = enemy.startPosition:lerp(enemy.endPosition, (time()-enemy.start)/enemy.end)

correct me if I’m wrong, but this should work just fine in your case (the variable names will need to be changed).

1 Like

Alright i will try this later, and confirm if it does work

correct me if i am wrong, but isnt start position and Position the same? or is it like the start position got added and your midway to the end

startPosition is the position at the spawn of the enemy. position is the current position.

i would make startposition the new waypoint they reached, because we got to take account to bezier curves for the enemy to rotate

1 Like

In that case, you might want to manipulate endPosition and the end time. Not really sure how this would be coded since I’ve never attempted something like this before, but if you’re able to make a bezier curve somewhat linear then you’re able to.

something is wrong with my code, can you help me on this? enemys dont move at all

                local enemy = workspace.Enemies.Active:FindFirstChild(v.ID)

		local node = workspace.Waypoints:FindFirstChild(v['Node'])
		local node2 = workspace.Waypoints:FindFirstChild(v['Node'] + 1)

		local distance = (enemy.PrimaryPart.Position - node.Position).Magnitude
		local current_time = workspace:GetServerTimeNow()
		local speed = enemyInfo[v["Name"]].Speed

		if v["Node"] == 2 then
			v["EndPos"] = node2.CFrame
			v["StartPos"] = node.CFrame

			v["StartTime"] = workspace:GetServerTimeNow()
			v["EndTime"] = distance/speed + workspace:GetServerTimeNow()
		elseif (enemy.PrimaryPart.Position - node2.Position).Magnitude < 0.1 then
			v["Node"] += 1

			node = workspace.Waypoints:FindFirstChild(v['Node'])
			node2 = workspace.Waypoints:FindFirstChild(v['Node'] + 1)
			v["EndPos"] = node2.CFrame
			v["StartPos"] = node.CFrame

			v["StartTime"] = workspace:GetServerTimeNow()
			v["EndTime"] = distance/speed + workspace:GetServerTimeNow()
		end	

		enemy.HumanoidRootPart.CFrame = v["StartPos"]:Lerp(v["EndPos"], (time()-v["StartTime"])/v["EndTime"])
1 Like

the node == 2 is the start node btw

1 Like

I forgot to say that endTime should not be adding the server time, it is just the duration.

1 Like
local enemy = workspace.Enemies.Active:FindFirstChild(clientInfo.EnemyHash)

	local node = workspace.Waypoints:FindFirstChild(clientInfo['Node'])
	local node2 = workspace.Waypoints:FindFirstChild(clientInfo['Node'] + 1)

	local distance = (enemy.PrimaryPart.Position - node.Position).Magnitude
	local current_time = workspace:GetServerTimeNow()
	local speed = clientInfo["Speed"]

	if clientInfo["Node"] == 2 then
		clientInfo["EndPos"] = node2.CFrame
		clientInfo["StartPos"] = node.CFrame

		clientInfo["StartTime"] = workspace:GetServerTimeNow()
		clientInfo["EndTime"] = distance/speed
	elseif (enemy.PrimaryPart.Position - node2.Position).Magnitude < 0.1 then
		clientInfo["Node"] += 1

		node = workspace.Waypoints:FindFirstChild(v['Node'])
		node2 = workspace.Waypoints:FindFirstChild(v['Node'] + 1)
		clientInfo["EndPos"] = node2.CFrame
		clientInfo["StartPos"] = node.CFrame

		clientInfo["StartTime"] = workspace:GetServerTimeNow()
		clientInfo["EndTime"] = distance/speed
	end	

	enemy.HumanoidRootPart.CFrame = clientInfo["StartPos"]:Lerp(clientInfo["EndPos"], (time()-clientInfo["StartTime"])/clientInfo["EndTime"])

still not working for some reaosn

well it glitches and sometimes moves

for i, clientInfo in pairs(Enemys) do
		local enemy = workspace.Enemies.Active:FindFirstChild(clientInfo.EnemyHash)

		local node = workspace.Waypoints:FindFirstChild(clientInfo['Node'])
		local node2 = workspace.Waypoints:FindFirstChild(clientInfo['Node'] + 1)

		local distance = (enemy.PrimaryPart.Position - node.Position).Magnitude
		local current_time = workspace:GetServerTimeNow()
		local speed = clientInfo["Speed"]
		
		if clientInfo["Node"] == 1 then
			clientInfo["EndPos"] = node.CFrame
			clientInfo["StartPos"] = node.CFrame

			clientInfo["StartTime"] = workspace:GetServerTimeNow()
			clientInfo["EndTime"] = distance/speed			
		elseif enemy.PrimaryPart.CFrame:ToWorldSpace():ToObjectSpace(node2.CFrame).Position.Z <= 0.1 then
			clientInfo["Node"] += 1

			node = workspace.Waypoints:FindFirstChild(clientInfo['Node'])
			node2 = workspace.Waypoints:FindFirstChild(clientInfo['Node'] + 1)
			clientInfo["EndPos"] = node2.CFrame
			clientInfo["StartPos"] = node.CFrame

			clientInfo["StartTime"] = workspace:GetServerTimeNow()
			clientInfo["EndTime"] = distance/speed
		end	

		enemy.HumanoidRootPart.CFrame = clientInfo["StartPos"]:Lerp(clientInfo["EndPos"], (workspace:GetServerTimeNow()-clientInfo["StartTime"])/clientInfo["EndTime"])
	end

So do you want a number that is the time(in seconds) from the start of the round/game or what?