Hi, i’m making dungeon crawler game, soo i have to make enemies like shooting npcs, fighting npcs, AOE units ect…, but i don’t know how to make the best performant system, i’d like to use part hitbox, when player touches part around area, then mobs agree and if stop touch they go back to their positions - it’s easy to make and i have a model of it
or another system that will work great if i would sort my workspace soo: go through players every 0.5 second, then check where character is, and if it’s in range, then sort them in array, choose closest, and attack.
Which way do you prefer? and what is more performant???
it is better to use the method of getting the distance between the enemy and the player as it will allow to easily add different distances, you won’t have to create any parts and you can get the nearest player for free. example:
MaxDistance = 100
Target = nil
function GetNearestPlayer()
shortestdistance = math.huge()
shortestcharacter = nil
for i,v in pairs(game.Players:GetChildren()) do
if v.Character then
local primarypart = v.Character.PrimaryPart
local distance = (primarypart.Position-<enemytorso>.Position).Magnitude
if distance <= MaxDistance and distance < shortestdistance then
shortestdistance = distance
shortestcharacter = v.Character
end
end
end
Target = shortestcharacter
return shortestdistance
end