# Blender Game Engine - How i can make my turrets aim the enemy that’s closest to the village?

i am trying to make a TD game. I need to make my turrets aim the enemy that’s closest to the village and i’m stuck. My turrets aim the last enemy that enters their firing zone instead of the enemy that’s closest to the village and are in their firing zone.

Any help?

.blend file here

======================================================================

Q:

what’s the difference module vs script in the logic bricks??

A:

script is for 1 object to use, module can be used by multiple objects, so instead of 1 tower you could connect 100 to it.

#Logick Bricks : (Always -> Python-Module track_to_closest.track -> Edit object - Track_to)
# and a property "track_distance" on the object using this script
# and place a property on the objects you want to track to: called track_to

def track(cont):

own         = cont.owner
scene       = own.scene
track_to    = cont.actuators['track_to']

ai = [obj for obj in scene.objects if 'track_to' in obj]

if ai:

closest_ai      = sorted(ai, key=lambda enemy: own.getDistanceTo(enemy))[0]
distance        = own.getDistanceTo(closest_ai)
max_distance    = own['track_distance']

if distance <= max_distance:

track_to.object = closest_ai
cont.activate(track_to)

else:
cont.deactivate(track_to)

• Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – Luciano Feb 13 at 10:33
• The person who answered the question only provided a link to the blend file (which is enough imo) with no additional information.......I tried to improve the answer by providing the blend file along with the python script and some more info.... hope it's better now – D_K Feb 13 at 20:24
• way better. The idea is to make Blender.se THE place to find information about Blender. When you post just a link and the link dies, the answer here loses all value. And the StackExchange model is Q&A, it's always better to have written explanations so others can understand without having to download files. Check How to Answer and the tour to learn more about the site. – Luciano Feb 14 at 0:05
• Thank you for the info/tips , I will take that into consideration in the future! CHEERS! :) – D_K Feb 14 at 22:17