Need help with tracking

Hi,

Can some one help me with writing a script for one object to tack another object? Currently, I have individuals that walk around randomly. What I would like to do is to make individual to go to the safe location after certain object approaches the person. I can not use IPO because individuals appear randomly at random places. Plus, I tried to use logic bricks with “Track to” option and that does not work as well, because of the always sensor that is connected to the individuals.

Thanks to all
Vlad

What does the always sensor have to do with the actuator not working properly? You need to explain what the problem is in more detail.

The “Track to” actuator works fine in 2.42a afaik. You don’t need to script the entire tracking action, you just need a script to handle the “Track to” actuator, so it performs as you want it to.

SOME EXAMPLES OF TRACK TO WITH CHARACTER

http://pochoblender.googlepages.com/MOUSEPRUEBA1.blend

http://pochoblender.googlepages.com/clickpointPOCHO2.rar

http://pochoblender.googlepages.com/personajeanimado3.rar

MY PAGE(SPANISH…ESPAÑOL DESDE LA ARGENTINA)
http://pochoblender.googlepages.com/archivosparabajar2
:smiley:

I just wrote a script to do this two days ago. Mine was for 2.25, but I’ve updated it here for 2.42a.

import GameLogic as g

cont = g.getCurrentController()
own = cont.getOwner()
ownPosi = own.getPosition()
trackTo = g.getCurrentScene().getObjectList()["OBtrackTo"]

left = cont.getActuator("MoveLeft")
right = cont.getActuator("MoveRight")
up = cont.getActuator("MoveForward")
down = cont.getActuator("MoveBackward")

trackPosi = trackTo.getPosition()

if trackPosi[0] > ownPosi[0]:
  g.addActiveActuator(right, 1)
else:
  g.addActiveActuator(right, 0)

if trackPosi[0] < ownPosi[0]:
  g.addActiveActuator(left, 1)
else:
  g.addActiveActuator(left, 0)

if trackPosi[1] > ownPosi[1]:
  g.addActiveActuator(up, 1)
else:
  g.addActiveActuator(up, 0)

if trackPosi[1] < ownPosi[1]:
  g.addActiveActuator(down, 1)
else:
  g.addActiveActuator(down, 0)

“own” in this script is the object that will track to the other object. “trackTo” is the object to track. Replace “OBtrackTo” with “OB” followed by the name of the object to be tracked to. I set up my logic bricks like this:

http://img199.imageshack.us/img199/1064/tracktologicfu9.jpg

If you want to see how this setup works, try my “Battle Balls” web plugin game. The computer uses this exact setup to chase after the player. As it is, the enemy will just circle around the player and push him around, since it can not reach the actual center. If you were using a plane for the object to come to rest on, this would work just fine, but if you want it to track to another object, put in some addition and subtraction in the lines that calculates the distance. Something like

 if trackPosi[0] < (ownPosi[0]-.6):

would work for the negative x if the object to track to was about 1 unit in diameter. Remember that you need to add .6 on the positive side instead of subtracting.

Edit: Web plugin version had been down, is working again. If you want to look at the .blend, you may. It’s for 2.41 and earlier. Won’t work right in 2.42.

Hope that helped. :slight_smile:

Blendenzo:

I am also using blender 2.25. I was just wondering if you could submit the tracking script for the version 2.25. If you could, I would really appreciate your help.

Thank you,
Vlad
Canada

Okay, sure. In 2.25, you need to use two scripts, since objects the getCurrentScene function hadn’t been implemented yet. Attach one script to the object that is to do the tracking, and the other to the object it will track to. The way I have it set up, you make a short script on the object to be tracked to that stores its position as a global variable (run it through an Always sensor):

import GameLogic as g

own = g.getCurrentController().getOwner()

g.trackPosi = own.getPosition()

The code for the object to do the tracking is pretty much the same as above, except instead of getting the position of the trackTo object through getCurrentScene().getObjectList(), you just read the global variable. Here’s the 2.25 version (changes highlighted):

 import GameLogic as g

cont = g.getCurrentController()
own = cont.getOwner()
ownPosi = own.getPosition()
<b># <i>deleted a line here</i></b>

left = cont.getActuator("MoveLeft")
right = cont.getActuator("MoveRight")
up = cont.getActuator("MoveForward")
down = cont.getActuator("MoveBackward")

<b> trackPosi = g.trackPosi</b>

if trackPosi[0] &gt; ownPosi[0]:
  g.addActiveActuator(right, 1)
else:
  g.addActiveActuator(right, 0)

if trackPosi[0] &lt; ownPosi[0]:
  g.addActiveActuator(left, 1)
else:
  g.addActiveActuator(left, 0)

if trackPosi[1] &gt; ownPosi[1]:
  g.addActiveActuator(up, 1)
else:
  g.addActiveActuator(up, 0)

if trackPosi[1] &lt; ownPosi[1]:
  g.addActiveActuator(down, 1)
else:
  g.addActiveActuator(down, 0) 

That’s all there is to it. Hope it works for you! :slight_smile: