I am trying to build a kind of tank, and for that I made a Body object, having a child Turret. The turret follows the mouse's X axis, and I would like the body of the tank to smoothly rotate towards the turret's rotation while in motion (ie. when w or s is pressed).
However the tank should not rotate while stopped, or moving sideways (a or d then).

I have this code:

import bge, math
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
obj = cont.owner
child = obj.children['Turret']

rot = obj.localOrientation.to_euler()
rotChild = child.localOrientation.to_euler()

rot[2] += rotChild[2] / 2
obj.localOrientation = rot.to_matrix()

which is connected in logic editor this way: script connection in logic editor

It works, but not as intended; the roation of the body is abrupt, and it only rotates for the instant I press the key (w or s) instead of the whole time the key is down.
I know my code is partly in fault, maybe I should access the body's angular velocity, but I don't know how to.

  • 1
    $\begingroup$ I would determine the relative orientation of the turret related to the tank body. The result should be left/right. Then let the tank turn to this direction. Consider a bias for "nearly straight" orientation. $\endgroup$ – Monster Aug 29 '16 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.