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