Track to closest, feed target to retical if Distance is less than 30

Ok this works,

All except the less than 30 part :frowning:

here is the working .blend

enjoy !

and help me :smiley:

Attachments

TrackAndShoot.blend (558 KB)

lol duh

Distance != dist !!

:smiley:


import bgecont = bge.logic.getCurrentController()
own = cont.owner


scene = bge.logic.getCurrentScene()
objects = scene.objects
distance=None
object=None
for ob in objects:
    if ob==own:
        continue
    if not own['property'] in ob:
        continue
    dist = own.getDistanceTo(ob)
    
    if distance==None:
        distance = dist
        object = ob
    if dist<distance:
        
        object = ob
        distance=dist
        
if not object==None:
    own.alignAxisToVect(own.getVectTo(object)[1],1,1)
    print(object)
    distance = own.getDistanceTo(object)
    if distance <=30:
        own['Target']=str(object)
        own['Distance']=<i><i><b>distance</b></i></i>
    else:
        own['Target']=str(own)
        own['Distance']=0
 
distance=None
.....
if distance==None:
        distance = dist
        object = ob
    if dist&lt;distance:

Um What? The first if statement wll always be true, and the second one will never be true.

There seem to be a couple other flaws in the design of your code.

I hacked it into a working script :smiley:

it works, and throws no errors.

that is in a for loop,

So it’s saying

if distance == none then find one! :smiley:

if dist < distance:
distance= dist
object = ob

what are you trying to do here? just trackto the closest or?

this is what i use for my tanks to let them shoot at eachother:

from bge import logic

range = 30


def closest(cont):
    
    scene_objects   = logic.getCurrentScene().objects
    own             = cont.owner
    
# always - script - edit object(trackto) actuator.    
    track_to = cont.actuators['trackTo']


    ai = [x for x in scene_objects if 'enemy' in x]
    dist = [own.getDistanceTo(x) for x in ai]
        
    if len(ai) is not 0:
        pair = min(zip(dist, ai))
        distance = own.getDistanceTo(pair[1])


        if distance &lt;= range:
            track_to.object = pair[1]
            cont.activate(track_to)

Above script tracks to the closest enemy property within given range

Look at the .blend,

the “track to” feed own[‘Target’] via logic to a retical, and the reticle is tracked to my another object, that is “TorqueTrackTo” and fires projectiles when the reticle touches it :smiley:

it’s a “smooth” turret

So object 1 - Feeds target to rectical

Object 2 tracks rectical

Gun - TorqueTracks Object 2

retical sends message “Fire” to gun when it collides with target :smiley: