#Handling enemy diffrentation on a track for a TD game

1 messages · Page 1 of 1 (latest)

hollow crown
#

In short form, I'm trying to make it so enemies dont just directly walk the track in my TD game but have some diffrentiation to it. The problem im facing is that, say a enemy starts at the start of the green line in my image, and wants to go to the curve. I have to figure out WHERE the enemy intersects.

So, what formula do I have to use to get that point? Basically, I have previouswaypoint, and waypoint (variables) which are practically the only two things I have. They are just parts that point to the next one. Heres a code snippet I tried but i didtn get anywhere with it.

local rootPart = clone.HumanoidRootPart
local heightOffset = Vector3.new(0, (-0.5) + clone:GetAttribute("Height"), 0)
local currentPos = rootPart.Position - heightOffset

-- my old flawed code which only works on some bends
local discrepancy = clone:GetAttribute("Discrepancy") -- basically the offset on the x axis

local targetPos = (waypoint:GetPivot()*CFrame.new(discrepancy,0,-discrepancy)).Position
local fromPos = (previouswaypoint:GetPivot()*CFrame.new(discrepancy,0,-discrepancy)).Position
bold waspBOT
#

studio** You are now Level 2! **studio

rough glade
#

Find the direction direction = (waypoint.Position - previouswaypoint.Position).Unit
Move from previous waypoint by how far you want
targetPos = previouswaypoint.Position + direction * distance

That’s it. Use targetPos for your enemy to go there.

If you want to move sideways too:

Find right side right = direction:Cross(Vector3.new(0,1,0)).Unit

Add sideways offset
targetPos = targetPos + right * sideOffset