Yeah, you can. Siegel actually wrote one for my BGHelper module - here’s the function, if it’s any use. You can just use the function below the definition.
def TrackTo(dest, obj = None, axis = 'y', time = 0, dimensions = "xyz"):
Track To function equivalent to the track to actuator.
Contributed by Siegel.
dest = destination object (or point) to track to
obj = source object to rotate; if set to None or omitted, it defaults to the object running the script
axis = which axis ('x', 'y', or 'z') to align the source object to the destrination point
time = time value to rotate the source object by (in seconds)
dimensions = specify which global axes to track on; you can select which ones to align to the object
if obj == None:
obj = logic.getCurrentController().owner
if axis == 'x':
axis = 0
elif axis == 'y':
axis = 1
elif axis == 'z':
axis = 2
vect = obj.getVectTo(dest)
if dimensions.count("x") == 0:
vect.x = 0
if dimensions.count("y") == 0:
vect.y = 0
if dimensions.count("z") == 0:
vect.z = 0
if time > 0:
obj.alignAxisToVect(vect, axis, 1.0/time)
Here’s an example:
from bge import logic
# TRACKTO FUNCTION DEFINITION HERE #
sce = logic.getCurrentController()
enemy = sce.objects['Enemy'] # Should probably be an enemy from a Near sensor or something
should track the script-calling object to the enemy, so that his +Y axis is facing the enemy. This hasn’t been tested, though.