I know there are lots of posts about this...

But most of the answers did not help or the blender example downloads have been delete.

Basically a script to change “Track To Selected Object” to “Track To Closest Object with Property”.

Like Warcraft 3 with Tower Defenses, once the unit comes with in range it attacks that unit till it goes out of range then it targets the next closest enemy unit.

I have been searching for a answer for a long time so this would mean a lot to me!

Thanks

You mean Object-A is tracking Object-B, after a few frame the Object-C comes more close to the Object-A then Object-A should track Object-C and leave the Track of Object-B? How many objects will be there ?

Yes that is correct. The objects will be created through “Add Object” with a Property that Object A senses and tracks the Object closes to it with the Property.

I can’t tell how many Objects with that Property within range of Object A will be at anytime but I wouldn’t think anymore then 10.

This one works fine with few objects (less than 15 or 20)

First, you need to get the list of objects with that property. You need a loop for that.

Then, you need to calculate the distance sort it (it’s organized in a list in a tuple like (integer distance, object) and pick the first one. After that you set the object in the track actuator and activate it.

Save it as a script, connect to an always sensor and a track actuator “trackact”.

import Gamelogic as g

c=g.getCurrentController()
o=c.getOwner()
sce=g.getCurrentScene()
obl=sce.getObjectList()
trackact=c.getActuator("trackact")#your track to actuator

objl = []

for object in obl:
    if hasattr(object, "yourpropnamehere"): #-put the propname you want (quoted) in "yourpropnamehere"
        dist = object.getDistanceTo(main) #-your player/whatever object is main
        objl.append((dist, object))

objl.sort()

trackobj = objl[0][1].getName()

trackact.setObject(trackobj)
g.addActiveActuator(trackact, 1)

Actually, there’s a little more to be added… using gamelogic variables.

till it goes out of range then it targets the next closest enemy unit.

until it’s out of range or until another unit gets closer?

I can help in in Animation but not in BGE ( Game Logic ) If you want I can create a python script to do so as I told in my previous post.

It is the unit that is closer

Thanks

That would be great, but I think B3D00 has already made the script.

Thanks for both your help!

So you mean, A (when a unit gets closer than the other one, not after X unit comes close and it doesn’t matter if Y or Z unit comes closer than it until X unit is out of a specific range?)
B (track to any close unit)?

Yes “B” is correct!

Thanks

Then the script it’s done (unless it has bugs, I haven’t tested it) :slight_smile:
You’re welcome.

You could do it all in logic bricks also, by using ray sensors and properties. It wouldn’t be difficult, but I see you already found help =P

But does this track to added objects? as far as I know that is still an issue…

That is right, the logic bricks don’t allow to track added objects.

Thanks

Thanks for your help, but I’m still having problems getting it to work. I’m properly just doing something very dumb.

Here’s a blend

If you could check it, that would be great!

I have tried a 250 Frame Animation as I told in my previous post. This is Preview Render, No Shadow to save the time? Will this match your reqirements?
My Python Script works for Frame 1 to 250. After each frame, it checks to closest object, and turns to Light towards that object.

http://video.google.com/videoplay?docid=-6517895145734397620

That looks great! Could that script work in BGE?

I am not comfirtable in BGE, but in Animation its working.

I need a script for game engine. Thanks for your support!

Did your problem solved?. Otherwise I will try my script in BGE,

No it won’t work in both. Different commands. I made that mistake years ago :no: