Python Track To and Steering Track to selection.

  • Python Track To
    [LIST=1]
  • Create a property called ‘target’ on the object running script. Set it to the property to track to
  • Create a text file “helper.py”
  • Copy this into the text file
  • Create and ALWAYS sensor, true pulse ([…])
  • Create a Python Contoller, set it to module, and type ‘helper.set_nearest_object’ -without the ‘’
  • Connect any actuator with an object field to the controller, and it will be updated to use the nearest object
import bge

def any_positive(cont):
    for sens in cont.sensors:
        if sens.positive:
            return True
    return False


def set_nearest_object(cont):
    if not any_positive(cont):
        return
    
    own = cont.owner
    
    try:
        prop_name = own['target']
    
    except KeyError:
        print('No property called "target" found, please specify target property')
        return
    
    try:
        closest = min([o for o in own.scene.objects if prop_name in o], key=own.getDistanceTo)
        
    except ValueError:
        print("No objects with required property ({}) found".format(prop_name))
        return
    
    for actuator in cont.actuators:
        if hasattr(actuator, "object"):
            actuator.object = closest  
            cont.activate(actuator)
        
        elif hasattr(actuator, "target"):
            actuator.target = closest
            cont.activate(actuator)             

[/LIST]

Track example:
track.blend (486 KB)

I dont get it. Sorry.

I have two cubes CubeA = Character, CubeB = Target

Create a property called ‘target’ on the object running script. Set it to the property to track to

I suppose CubeB (target) has the ‘target’ property. Running script? What script? module.py?

Create a text file “module.py”
Copy this into the text file

Done!

Create and ALWAYS sensor, true pulse ([...])
Create a Python Controller, set it to module, and type 'module.trackNearestProperty;

Code:

In what object CubeA or CubeB?

'module.trackNearestProperty;
With ’ and ;?

Sorry but I try to make this work, but I can’t do. Maybe I dont understand perfectly the instructions because I dont know much English.

If you just want to track to an object, use the track to actuator. If not You add a property called target on the object that does the tracking, and set its value to the name of the property to track to. Then, add an always true pulse sensor (on the object that does the tracking) and connect that to a python controller. Set the module in the box to module.trackNearestProperty
Then, to each object to track to, add a property with the name that you typed in the target property… that should work

Still trying.
http://www.pasteall.org/blend/9147

Very cool and helpful!

Sorry, fixed my code, i renamed it for some reason 0.o

testing.blend (338 KB)
Here’s your file.

It’s working. Thanx.

Where was the error?

I renamed the script by accident

On your second script i want it to activate the steering actuator.Could you please give me that?

I’ve updated the code on the first post.

Doesnt work could you post a blend please?

example2.blend (625 KB)
Here you go.

Hi. Cool example.
Can find the nearest navmesh?

What do you mean tort?

Say: the game has two stages in the different scenes. How to use two different navmesh?
http://i5.pixs.ru/storage/6/4/5/Screenjpg_3849474_3212645.jpg
http://pixs.ru/showimage/Screenjpg_3849474_3212645.jpg

Why dont you use a state engine and near or radar sensor to switch between states and a mesh object with a property on it.That would allow you to switch between navigation meshes.

An example would.

Heres the blend raycast detour switching states by propertys blend.
Take a look at it and see if you can understand it.Explanation is in
blender text editor.:yes:switch states raycast detour.blend (93.7 KB)

I think it would be nice if the script for raycast detour could be set to track to the nearest property in the direction you choose x,-x,y-,y,z-,z using a state engine.I am trying to make halo type ai system.