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?


I believe that your question has already been posted and answered on blenderartists here

.blend file here


Additional info:


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


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

|improve this answer|||||
  • 1
    $\begingroup$ 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. $\endgroup$ – Luciano Feb 13 at 10:33
  • $\begingroup$ 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 $\endgroup$ – D_K Feb 13 at 20:24
  • $\begingroup$ 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. $\endgroup$ – Luciano Feb 14 at 0:05
  • $\begingroup$ Thank you for the info/tips , I will take that into consideration in the future! CHEERS! :) $\endgroup$ – D_K Feb 14 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.