Tracking to a created object

So, what I’m looking for is to have a cube follow a target that is created by an empty. My problem is that when the empty spawns the target, the cube no longer follows it. I assume this is because the name of the spawned target differs from the original and therefore no longer fits the track to actuator. Now what I guess I’m really asking is there any way to either:

  • Track to a property

  • Change the name of an object realtime

  • Change the track to target realtime

Ultimately my goal is to make a dynamically choosing thingy. Essentially, I would have multiple animation targets and the cube would choose a random number and spawn a corresponding target which it would track to. It may be a bit confusing but if you could answer any of the 3 questions above I would be very great full. I’ve really hit a brick wall and could do with the help. :spin:

Ok, so I found something that will work. Well at least it tracks to a property instead of a object name. However, there are a couple of problems with it.

  • I don’t know Python so I don’t know how to change the property in the script (see below for more description)
  • It’s an old script and gives deprecation errors

What I mean by number one is that I want the script to look at the owners property list for the track to property target in this line
“prop = ‘Target’”
I’ll post the example blend so you can get this in context. I’m sure it’s possible to do do this. Once more, for clarity, When the python script looks for the property to track to, I want it to look at the script owner’s property list to find the property that the owner should track to.

The Script

cont = GameLogic.getCurrentController()
own = cont.getOwner()

scn = GameLogic.getCurrentScene()
ob = scn.getObjectList()
track_ob = cont.getActuator("track")
    
def trackToClosest(cont):
    own = cont.owner
    prop = 'Target'
    obl = [ob for ob in scn.getObjectList() if ob.has_key(prop)]
    
    closest = [obl[0], own.getDistanceTo(obl[0])]
    for ob in obl[1:]:
        dis = own.getDistanceTo(ob)
        if  dis < closest[1]:
           closest = [ob, dis]

    track_ob.setObject(closest[0])
    GameLogic.addActiveActuator(track_ob, 1)


trackToClosest(cont)

And an example .Blend file

PS, I don’t need the track to closest part of the script but since I don’t know python it would be nice if you could show me how to remove it so that the script still works.

Attachments

track to closest.blend (132 KB)