How to make object track to another using using only z axis

I need a code to make “own” track “objectB” using only z axis.
The Track axis has to be -y and the up axis has to be z.

But the thing is, when the object that contains the script rotates (because it’s tracking Object B), it can only rotate around z axis. So that -y points object B, only using z axis.

Try running this with an always sensor and true triggering enabled:


import bge 
import mathutils


cont = bge.logic.getCurrentController()
owner = cont.owner


scene = bge.logic.getCurrentScene()


desiredRot = owner.getVectTo([scene.objects['objectB'].worldPosition.x, scene.objects['objectB'].worldPosition.y, 0])


owner.alignAxisToVect(-desiredRot[1], 1, 1)

Basically, you want to get the vector to that object, but only for x and y. You then align your object with that vector.
Afterwards, you specify that you want to align along the y axis, and finally negate the vector to align along the
-y axis.

the script will work, but the object will start to wobble/tilt on its y axis. You also seen to keep z pointed up or -z pointed down.

owner.alignAxisToVect(Vector([0,0,1]),2)

Ah, thank you. That’s right, unless the object is already aligned along the z axis, the object will tilt, so you should insert superflip’s code.