Left right patroling

how to make EnemyShip to go left and then right (like he is patroling) and then back again

This is one of the things in GDevelop platformer tutorial, but if you just want to use one Square object (or what the object that you use to change direction):

  • add a “direction” object variable to EnemyShip, can be “left” or “right” by default. if “right”, add instant force of 100 to the ship, vice versa for left.
  • when ENemyShip is in collision with Square, add another condition to check for EnemyShip’s X position, and check if it is to the left (less than) or right (more than) Square’s X position.
    If EnemyShip.X() > Square.X() | Change object variable direction of EnemyShip to "Left"
    Then to change direction to Right…
    ` If EnemyShip.X() < Square.X() | Change object variable direction of EnemyShip to “Right”


I have created an extension called “Timed back and forth” or something.
It should solve your problem :slight_smile:
To use it, open the object properties, go to behavior, and search “timed”. Then install and configure as you like. :+1:

1 Like

ok, I’ll try it
thank you
I did menage to make something,…but its not very elegant :grin:

1 Like

nice job on using angle! but, aren’t the conditions for the two events the same?

well the problem was
when player comes close to enemy should start patroling
and enemy (ship) didnt go right just left (see above 1st picture) (I have tryied with stop object and without)
so I delete ship in moment when it should turn
and create a new fliped image (clean)
I have a filing that Grouks extension will do that much better,…I havent tryed it yet

I see, that’s… quite original in doing that actually, although it might cause you problems later on as you add more features.
Either his extension (I haven’t tried either), or you can try looking at this (basically what I said, but explained better): Add enemies to your platformer game [GDevelop wiki]
Look around “Move the enemy left and right” part, hope you understand it, and later decide which one is best.

1 Like