How can I track to nearest object with property enemy?

I want to track to nearest object with the property enemy

preferly using just gamelogic

if not then is there even a script for this

There’s nothing to really get you the nearest object, not that I know of anyway. You would have to get the hit object list of the near sensor in python and then check distances. Your odds of getting a script that really fit your needs are minimal. Maybe you’ll find one. By setting up a range of near sensors short, medium, long, you could get something like that, and that’s what I would recomend. Even then, you would have to use a simple python script to choose which one to follow.

this is a task that very few people attempt, although i do recall soeone making a file that is kinda like packman and eats up all the dotts around it by getting the closest ones first, unfortunately i have no idea how they did it or how it works

i have done a shooting game where AI enemies track to the nearest enemy, but this method wont let you track on to added objects, all bots where already placed into the scene initially
you can see the thread and DL the game here

the following is a “copy and paste” from that thread
How the AI works

(if you’re not a bit familiar with python, i woudlnt attempt this, you can learn more about python with the links from the sticky tuturials)

at the moment, every character gets their own script and annouces their xyz location to GameLogic, ei

GameLogic.Red1 = own.getPosition()

everyones script will also compare their distances with all other players using their position and its own position


Each bot has 4 track to actuators to track on to the 4 opponents, using if statments you can activate the correct track to actuator and have the bot look at the closest enemy. refresh rate of this script is about every 2 seconds

the ai bot jumps to dodge shots when an enemy bullet is seen with a radar infront of it. it also jumps when it is unable to see the closest oppoent after about 15seconds, so that if theres a wall it can jump over it.

the bots have random motions, side strafe left right, advance closer or stop, when far from opponent, it will run closer more and strafe less, when closer to opponent, it will strafe more and advance less, this can be done with math.getRandomFloat() and using if statments

using a ray, if it sees an enemy property such as “blue” it will shoot.