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!
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?
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)?
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.