How can i make enemy detects player performant?

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???

2 Likes

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
2 Likes

with it you just need to call the function and check if there is a target, if yes then chase the target but if not then go back to the original point

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.