# With a button pressed,how do I get a character to move left and then move right once that character hits a wall, infinitely?

I’m having so much trouble with this. I was able to succeed (with variables and such) in moving the character right and then make it move left once it touches the right wall and then finally move right again once it touches the left wall but it never wants to loop and keep doing these actions (moving right,hit a wall,moving left,hit a wall, repeat) and I really don’t understand how to do it. If anybody can help me I really appreciate it

If you are using a velocity variable to move the character (like 400 to move right, -400 to move left), for the event where the character collides with a wall, you can change that variable to be multiplied by -1. This makes a 400 = -400, and vice versa.
In the same condition, also put a condition called Trigger Once so the event does not occur multiple times.

By the way I have the dark theme applied and also I’m no expert at this but anyways, when the character hits the right wall, the character turns left and keeps going until it hits the left wall in which it turns right which of course I expect it to hit the right wall and turn left yet again like it did before obviously.The problem is though, is that it clips right past the right wall and then never turns left at this point for some strange reason… So yeah, and thank you so much for the response though I appreciate any help I can get with this.

Firstly, you’re welcome!

In both your collision events with RightWall and LeftWall, try using “set to” instead of add and subtract. So for “Character is in collision with RightWall” event you set Panning variable to 1, and the other way around for LeftWall. Your character might’ve collided multiple times, causing the Panning variable to be added/subtracted more than once.

Also, for the second event “Move Character away from LeftWall”, it’s inverted; you’d want to separate Character from RightWall if they are colliding with it, and the same with the other event.
Try that and tell how it goes.

Honestly I didn’t notice that I had it inverted. After trying out many things I must’ve became side tracked and didn’t notice somehow. Sadly though, it still does the same thing it did before unfortunately.

Okay, some more suggestions:
Does the first event in your screenshot (where you set Panning to 0) have a condition? I assume it’s “At the beginning of scene” though, which is a good condition. If it has no condition though you might be setting Panning to 0 every frame…!
Can you move all last three events to the left, so they are not sub events? The line to their left indicate they are sub events, and all three will only run if the global variable Panning = 0.

I did it and made it work exactly the way I wanted it to and I also got rid of the global variable = 0 and or 1 right below where it said character is in collision with the walls just to make it work better, oh and just so you know I did have a condition at the beginning of the scene. Thank you for your help, it actually helped me out especially since you told me to move the last 3 events, which I did but I added some more changes to make it work properly. Again I appreciate it and here’s what the final events look like.

2 Likes