Lock translation along x axis. (2D game)


Hi. As you can see I’m currently working on a 2D game (that will probably never finish but yeah… :eyebrowlift2:)
I’m curious if there is a way to lock translation along (in this case) the X axis.
The ‘archer’ consists of a invisible box with ‘Character’ as physics type and a 2d sprite with no collision.
Sometimes the archer goes backwards (with backwards I mean that he moves on the X axis, not that he moves to the left) when he is hit by a arrow or something else. Of course you can’t notice when he moves on the x axis since it is a 2d game, but it causes some problems with collisions.
I can’t find something on the internet that says how I can prevent the archer from moving along the x axis.
I hope you can help me.
Thanks.

There’s buttons in the game physics panel to lock translation (movement) and rotation on the X, Y, or Z axes. You might, though, want to use Python to stop movement and lock the position. That way you can be sure by stopping motion and locking the objects’ “depth” that they’ll never move accidentally.

Thanks for your reply. That first option where you’re talking about is not available in the character physics type. I know I can switch to rigid body but than step height and jump force will disappear and I really love the jump option. So I prefer your second option, but I’m a noob at python, so can you help me creating a code for that? :slight_smile:

Yeah, okay.



from bge import logic

cont = logic.getCurrentController()  ## Get the Python controller running this script.

obj = cont.owner  ## Get the object that has the controller.

obj.worldPosition.x = 0  ## Lock the object's position on the X-axis to 0.

obj.worldLinearVelocity.x = 0  ## Lock the object's velocity on the X-axis to 0.


Untested, but this should work. If it doesn’t, examine the error messages in the console and see why.

Thanks !!
It works :slight_smile:

Btw I love your game project Gearend :slight_smile:

Glad it works.

And thanks!