# "hallway" movement

This is hard to explain.

Basically, I’m making a game that plays a bit like pac-man, but there’s a problem.

The character moves automatically like pac-man, and when you press one of the arrows, it changes direction.

I’m trying to set it so the character can only change directions when there’s an opening that way.

Example:

Here are my events:

What do I do?

Could be

• if Miko variable direction = 0
• Miko don’t colliding with wall, press Right.
• Miko colliding with wall, do Miko variable choice = RandomInRange(0,1)
• if Miko Variable choice = 0, then Miko variable direction = 2
• if Miko Variable choice = 1, then Miko variable direction = 3

This way when the character collides with a wall, it will decide if going up or down.

A more intuitive approach could be:

• if Miko variable direction = 0
• Miko don’t colliding with wall, press Right.
• Miko colliding with wall, do Miko variable direction = 2

Normally we associate right and up, and left and down together.

Another way could be to test the possible paths for Miko and then choose one, or use special objects meant to tell Miko which paths are open so it can have a choice.

1 Like

@oscuridad666 Thank you so much, you helped me solve it!

1 Like

I would look at adding 4 points on the sprite, each a few pixels outside the sprite border. One for the top, bottom, left and right edges. When the player presses a direction key, check that the point for that direction isn’t on a wall object.

2 Likes

in my opinion raycasting would be a good way to do it like if raycast from player angle -90(this is up if I remember correctly) distance 8 would work in theory

2 Likes

Raycasting is a viable option, though keep in mind it is more processor hungry.

1 Like

oh didnt think of that lol

1 Like

I tried both of them but they didn’t work for me
here is the first one

You need to give more context what are you referring to when you say “both of them”?

It looks like you are using oscuridad666’s reply. I don’t think that exact answer would have solved the problem. Because when the player is in collision with the wall, it’s not being moved away, and so stays in collision with the wall.

Also, that random in range and then changing the direction value is not the best way to do it.

Try the following - it should work better :