Can I TrackTo with python..no actuator

Hi.

The Title is kind of it. I was wondering if I can activate (Edit Object)trackTo) in my python script without having to add an actuator for it. I already use python script for everything and it just adds and activates my (EDIT OBJECT: TRACK TO) actuator. If possible Id like to do this right within my script

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[1].x = 0
    if dimensions.count("y") == 0:
        vect[1].y = 0
    if dimensions.count("z") == 0:
        vect[1].z = 0
        
    if time > 0:
        obj.alignAxisToVect(vect[1], axis, 1.0/time)
    else:
        obj.alignAxisToVect(vect[1], axis)


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

TrackTo(enemy)


should track the script-calling object to the enemy, so that his +Y axis is facing the enemy. This hasn’t been tested, though.

Just for information:

There is no benefit in replacing the processing of an actuator with the same processing in Python. It simply eats more processing power.
It makes sense if the actuator does not perform the processing as you need it.
E.g. You need to change the orientation immediately because the follow-up code depends on this change.

I think you’re best off with obj.alignAxisToVect(other.worldPosition - obj.worldPosition, 2, 0.1) .
(Though from qhat I have seen: If a TrackTo Actuator can be used, then use it! It eats far less Logic!)


The given Line would, by the Way, work like setting the TrackTo Actuator to 3D.
For the generally wanted 2D TrackTo Effect, the Vector would have to be tweaked a Bit, so that the Z-Difference remains 0. (which might be easy to get but a Bit more ugly-looking)

Yeah after looking at all that, I think ill stick to my actuator and 2 lines of code. :smiley:

Thanks for the help