Tower Defense | Enemy teleporting after rotating

  1. What do you want to achieve? I want to make a simple enemy rotating before reaching current/target node like TDS (Tower Defense Simulator).

  2. What is the issue? it is working perfectly but after the bezier (I use Bezier module from B Ricey Tutorial) ends it just teleports to some studs forward, I don’t know why.

  3. What solutions have you tried so far? Already tried to make all nodes bezier into one, but still no

B Ricey modified module :

function quadraticBezier(t, p0, p1, p2)
	return (1 - t)^2 * p0 + 2 * (1 - t) * t * p1 + t^2 * p2
end

local function cubicBezier(t, p0, p1, p2, p3)
	return (1 - t)^3*p0 + 3*(1 - t)^2*t*p1 + 3*(1 - t)*t^2*p2 + t^3*p3
end

local function createLUT(numSegments, self, ...)
	local distSum = 0
	local sums = {}
	local step = 1/numSegments
	local points = unpack({ ... })

	for i = 0, 1, step do
		local firstPoint = self._bezierType(i, unpack(points))
		local secondPoint = self._bezierType(i + step, unpack(points))
		local dist = (secondPoint - firstPoint).Magnitude
		table.insert(sums, distSum)
		distSum += dist
	end
	return sums 
end

local function remap(n, oldMin, oldMax, min, max)
	return (min + ((max-min) * ((n - oldMin) / (oldMax - oldMin))))
end

local Bezier = {}
Bezier.__index = Bezier

function Bezier.new(numSegments, style, points)
	local self = setmetatable({}, Bezier)
	self._points = points
	self._bezierType = (style == 1 and quadraticBezier or cubicBezier)
	self._distLUT = createLUT(numSegments, self, points)
	print(self)
	
	return self 
end

function Bezier.compressPoints(numSegments, style, ...)
	local points = { ... }
	
	local self = setmetatable({}, Bezier)
	self._bezierType = (style == 1 and quadraticBezier or cubicBezier)
	
	for _, point in pairs(points) do
		points[#points + 1] = point
	end	
	
	self._points = points
	self._distLUT = createLUT(numSegments, self, points)

	return self 
end

function Bezier:Calc(t)
	local LUT = self._distLUT
	local arcLength = LUT[#LUT]
	local targetDist = arcLength * t

	for i, dist in ipairs(LUT) do
		local nextDist = LUT[i+1]
		
		if not nextDist then
			local adjustedT = remap(targetDist, dist, dist, i/#LUT, i/#LUT)
			return self._bezierType(adjustedT, unpack(self._points))
		end
		
		if targetDist >= dist and targetDist < nextDist then
			local adjustedT = remap(targetDist, dist, nextDist, i/#LUT, (i+1)/#LUT)
			return self._bezierType(adjustedT, unpack(self._points))
		end
	end

end

return Bezier

to Move them i put a code into a RenderStepped, code :

Data.Moved += Delta * Data.Model:GetAttribute("Speed") / (PreviousNode - CurrentNode).Magnitude

local Lerp = Data.Points:Calc(Data.Moved)
local LookAt = Data.Points:Calc(Data.Moved * 1.01)

Data.Model:PivotTo(CFrame.new(Lerp, LookAt))

if someone know how to fix it, pls help me :sob sob

o ya the compressPoints in the Bezier module is useless and unused

sorry for bumping, but does someone know any suggestion/way to fix it?

If I were you, I would do RenderStepped and check for its position and check if it actually jumps or just takes a moment to render after calculating a bezier everytime. I also would attempt to figure out the different turning points of the path to tell the enemies which curves to turn at for square like paths, it would reduce a lot of lag.

1 Like