Make object track or seek closest?

Hey. you know how you can make an object seek or track another object.

lets say i have an enemy in my game and i want him to attack multiple targets, how can i make him attack the closest?

Because default all he will do i try to track both and he will get stucked somehere in between :stuck_out_tongue:

So is this possible?

Or maby is it possible to make him have a ray or radar, and then track whatever objetcs gets in there?
at least if it has a specific property?

In python you would have a list of enemies (gameObj) and sort it by distance ( closest to furthest ).
Then make the active target to be the listOfEnemy[0] (first in list/closest enemy).

I would not suggest pure logic bricks for this.

Damn, i dont know python :confused:
Also the there Will be a random number of enemis every time so i cant make a list

so you make a new list every time the number of enemies changes (adding enemies, removing enemies).

Try this

TrackToNearest.blend (517 KB)

or this one:


def track(cont):
    
    own         = cont.owner
    scene       = own.scene    
    track_to    = cont.actuators['track_to']
    
    ai = [x for x in scene.objects if 'track_to' in x]
    dist = [own.getDistanceTo(x) for x in ai]
        
    if len(ai) != 0:
        pair = min(zip(dist, ai))
        track_to.object = pair[1]
        cont.activate(track_to)

track_to_closest.blend (949 KB)

cotax, that may work, but you may want something easier for someone new to coding

like

import bge

cont = bge.logic.getCurrentController()
trackTo = cont.actuators['Track']
own = cont.owner

Elist=[]

for objects in bge.logic.getCurrentScene().objects:
    EList.append(objects)
dist = 1000
target="Empty"
for enemy in EList:
    if own.getDistanceTo(enemy)< dist:
        target = enemy
        dist = own.getDistanceTo(enemy)

if target !="Empty":
    trackTo.target=target

untested

Wow,@BluePrintRandom, that is a lot worse.
Why would you think that is better?

Almost what I imagined!

Sorting this way would shorten the code and teach crucial part of using python built in sorting.
closest = sorted(ai, key=lambda enemy: own.getDistanceTo(enemy))

Second idea would be to use the near sensor’s like RockyMadio, which neat,
but having less bricks and more control over distance calculation (A* path len) is neater.

Ok thats easier, and check distance isn’t that hard to put into, also we can let it stop tracking.

So this is what you get:

# always -> python-module-track_to_closest.track-> edit object - trackto# use a property "track_distance" on the object using this script




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)

And a .Blend:
track_to_closest_v2.blend (951 KB)

its better code (yours)
in fact your code is what you would need for a dynamic lighting manager
(some of it :slight_smile: )

but Mine is easier for a newb I think

(less advanced concepts)