Gun Track to any instance?

So I have a tank that automatically aims at zombies. The problem is that the tank turret aims as the original zombie in another inactive layer…How can I get the turret to follow any instance of the zombies that an empty spawns?

property initial value
Target = “Empty”
On -Boolean ##toggle on and off with getting in and out.

if property On = True--------------------python 1
30 - ticks

if property On = True
5 ticks----------------python 2

python 1 - gather list

import bge
scene= bge.logic.getCurrentScene()
cont= bge.logic.getCurrentContoller()
own=cont.owner



## look through all items in scene after
## initiating a dictionary
own['distanceList]=[]
own['targetList']=[]
for objects in scene.objects:
    if 'Zombie' in objects:
        own['distanceList']+=[objects.getDistanceTo(own)]
        own['targetList']+=[objects]
    index=index+1   

python 2


import bge
scene= bge.logic.getCurrentScene()
cont= bge.logic.getCurrentContoller()
own=cont.owner


index=0
##Range
D=600
own['Target']="Empty"
for items in own['targetList']:
    distanceList=own['distanceList']
    targetList=own['targetList']
    if distanceList[index']<D:
        own['Target']=targetList[index]

then you will need to adapt a “gamelogic simple”

to do this

if Target !=“Empty”-------------python-------------track to

check it

this does formentioned list building and target selection

I want to help you set track to

Attachments

ListAndSortList (2).blend (448 KB)

How to: deal with TrackTo and added Objects

here is track to closest, with exception prevention

Space bar ends targets sequentially

property is better then instance, so you can have many zombies, tracked by same system

Attachments

TrackToClosest.blend (464 KB)

Thanks guys. Got it working :slight_smile: