Hi, I am making a Tower Defense Simulator game. I am currently using the “Closest” Targetting system, but it is not fun to play with. So I wanted to do a first targetting system but I am not sure how to. I tried looking at devforums but none of them helped. Does anyone know how to do it? I was thinking of making when the enemy touches the range, it puts it inside a table and the tower shoots at it only until its dead, but some fast enemies can get past it, so it wouldn’t work as well.
I remember doing a project like this in the past, however it’s on a separate account that I can’t access at the moment
Not going to write the code but here’s a suggestion:
Get all enemies in tower’s range
Get the path point that each enemy last passed (can use IntValues in enemy models for this)
Get the distance of each enemy on the farthest path point from the next path point
Do some comparing and bingo, you found the first enemy closest to the end
I don’t even know but something like this I guess.
local Hero = script.Parent
local Mobs = Folder:GetChildren()
local Target, NewDistance = Nil, 0
For i, Mob in Pairs(Mobs) do
if Mob.Position - Hero.Position).Magnitude > NewDistance then
Target = Mob
end
end
I’d be happy to help you with that! It sounds like you want to implement a “first target” system in your Tower Defense Simulator game, where the tower shoots at the first enemy that enters its range.
One way to approach this is to create a table to store the enemies that are within the tower’s range. When an enemy enters the range, you can add it to the table. Then, you can have the tower shoot at the first enemy in the table until it’s dead.
Here’s some sample code in Lua to get you started:
local function addEnemyToRange(enemy)
table.insert(enemiesInRange, enemy)
end
local function removeEnemyFromRange(enemy)
for i, e in ipairs(enemiesInRange) do
if e == enemy then
table.remove(enemiesInRange, i)
break
end
end
end
local function towerShoot()
if #enemiesInRange > 0 then
local target = enemiesInRange[1]
removeEnemyFromRange(target)
end
end
-- This is just a basic example, and you'll need to modify it to fit your specific game. You may also need to add additional logic to handle cases where multiple enemies enter the range at the same time, or where enemies move quickly through the range.
I hope this helps