How do I make a horizontal movement behavior?

I am trying to make a custom movement behavior where the player can only move left and right to dodge bullets falling on top of them using the arrow keys. For some reason, I just can’t figure it out. If someone could help me that would be awesome. (new to gdevelop)

Hi there :smiley:
So first you want to add a behavior to your player sprite, a Platformer Character type of behavior.

So after you have done that, in the behavior properties, deselect the Default controls option, since it adds movement for all arrow keys. But we only want left and right.

Then add the left and right movement using events.

Add a condition to detect left keyboard key press.
To add this, click Add a condition->Keyboard->Key Pressed and then assign the player object and the designated key, left arrow key in our case.


Now to add a movement. Click Add an action->Platform Behavior->Controls->Simulate left key press.


The final event will look something like this:

Similarly add another event for the right key condition and action :smiley:

Hope it helped. If you have any suggestions, feel free to ask :wink:

1 Like

Where would I put the events? This doesn’t seem to be working for me in game.

So first you want to add a behavior to your player sprite, a Platformer Character type of behavior.

I don’t think a Platformer kind of behavior is a good idea if you want the character to only move left-to-right without the need to jump on platforms or anything of the sort. Specially if there are no platforms to jump on in the first place. Wouldn’t the character just fall?
I’d suggest using the Top-Down Movement behavior instead - and toggling off the “Default Controls” option in the settings of the Behavior. You will see it when you add it.

Once that’s done, go to the Scene Events - which is the tab right next to the Scene Editor where you have you objects - and add the two events necessary.

If Left key is pressed
  simulate pressing Left for Player

If Right key is pressed
  simulate pressing Right for Player

The “Key Pressed” condition can be found in “Keyboard > Key Pressed”.
The “Simulate Key Press” action can be found in “Top-Down Movement > Controls > Simulate Left Key Press” (and the same goes for the Right Key press).

If you want to create a Custom Behavior though, as opposed to just using events, there’s a whole more to it. There’s an entire page on the Wiki about that, I hope it helps! It does get more complicated though. If you’re a beginner, your issue could really be more easily solved just by using regular Events and/or putting them in an External Event so you can use them in more than one scene.

I hope this helps!

1 Like

Ahhh nevermind its still not working…Idk what it is

Here’s what the scene events tab looks like to me:

Can you share a screenshot of your current events? (Nevermind, just saw it, one second)

What happens when you preview it, exactly? Does the character just fall? Or does it not move at all?

Does not move at all, screenshot above
EDIT: Going to try solution 1sec

Ooooooh! You need to separate those two into two different events!

You have two contradicting actions in the same event. Those should be separated into two different event blocks. Create one event for each of these!

1 Like

IT WORKS!!! Man this took wayyyy too long to figure out lol. Thanks for help though <3
EDIT: off to bed now lol


Yay! I’m glad it worked! And you’re welcome. ^^


Yea the problem was due to the conditions in the same event :wink:

1 Like