How to make character go back and forth

i want to make a game that will simulate the player going back and forth. how can i make it so that a tap makes the player go in the opposite direction but if the player is already traveling in one direction he goes the other way? i’m very new to this program.

There will be a few ways to achieve this. I very highly recommend completing at least the Platformer Tutorial for Gdevelop 5, as it will cover all the basics you need to really get going with Gdevelop :slight_smile:

Even if you don’t want to make a platformer, most of the concepts will be transferable to other game types.


thank you, i finished it. :slight_smile:
i’m still not sure if i can set a condition that will see if the character is moving right, so it can move left, and vice versa :confused:

You could do this by making two player object variables


if positionX < player.X() - set object variable(direction) to "right"
if positionX > player.X() - set object variable(direction) to "left"
no condition - set object variable(positionX) to  to player.X()

What this should do is take the player’s x position, and then on the next frame will compare that position to the player’s current position. If the variable is less than the player’s, it means the object is moving right, if it is larger, it means the object is moving left. (I think i got the left and right in the correct order)

Currently away from my gdevelop computer so I cannot test this, but I hope it helps.

1 Like

thank you for the help, sorry to inconvenience you if you’re away from your pc :slight_smile:
i’m just confused to how this works. what role does positionX play in this? what is its value? won’t i also have to add a force of a certain p/s?

Apologies, I thought you just needed to know the current direction.

Also, I forgot gdevelop had an online version, so I quickly whipped this up.

The player object will need three variables

And these actions will move the object left and right when touched, or in my case when the mouse button was released.

Hope this helps :slight_smile:


Although it has occurred to me that if you’re making a platformer, the code will be different. What kind of game are you working on?


this has helped greatly, thank you.
i am making a single control game where the player moves left and right only using mouseclick or touch.

1 Like

hey, quick question
what is positionX equal to?

1 Like

Ahoy, the positionX variable takes the player’s X coordinate from the current frame.

The position of the event in the eventsheet is important as it has to be below the condition where the comparison is made.

In Gdevelop, all events are processed each frame from top to bottom, so anything down below will get processed last. So the positionX variable stores the player’s X coordinate and the end of the frame, and in the next frame, that value will be used to compare the player’s current X value.

Hope this helps :slight_smile:


sorry to be replying so late, but just a quick question: what is the expressing that i should put in the value box for positionX? :slight_smile:
it’s in the image up there

1 Like

You don’t need an initial value for that variable as it will be set by the very last event in the example

1 Like

last reply, promise :slight_smile:
the code doesn’t appear to be working, have i made any errors?
(variables too)

You could try switching the Left Mouse button is down, to is released. That’s the only thing I did differently to your supplied code. I did test my example so I know it worked :man_shrugging: , so if something else is happening I’m not quite sure what could be going on.

With the Left Mouse Button is down conditions, the actions could be getting executed multiple times and conflicting with each other.

Hope you get to the bottom of it.

1 Like

would you care to share your code? perhaps it’s an error i’ve made in some syntax that’s not visible in the screenshots.

I made the example on the online version of GDevelop and did not keep a copy sorry, but the entire code I used was in the screenshot. I will see if I can make another example.

1 Like

Oh wait, I spotted it. Your last condition needs to have position updog.X() rather than player.X() which gets the X value of your object. I didn’t realise the error until i tried remaking it :slight_smile:

1 Like

no it doesn’t because the platformer uses default controls. The idea is to get the user to put in his own controls so he can see how it works. Honestly all the tutorials around here are garbage.

Thanks for the feedback codefree. I did not make this tutorial, but I found it helped me a lot in understanding the fundamentals of the engine. As GDevelop is community driven, the tutorials and documentation are also made by the community. If you have any particular feedback on how improvements can be made, you can share your thoughts on the forum or even contribute your own tutorials. :slight_smile:

There is another problem in the actions there:


The one on the top is correct. It sets a number to the variable speed of updog.
The one on the bottom is using the wrong action. It sets a text to the variable speed of updog.

1 Like