I'm creating a pacman game copy and I set up my movement. But there's a problem. My character can try to go up whenever I press but in the original you cannot go if there's a wall above you or your character won't fit.

enter image description here

For example in this situation you can still go up but you'll bounce off wall. So my question is how to prevent character from movement if there's a wall or it won't fit there.

  • \$\begingroup\$ I think you want a circle cast there \$\endgroup\$
    – Bálint
    Aug 13, 2017 at 13:10
  • \$\begingroup\$ I knew it will be something with raycasting. The thing is I don't understand how they work so I asked here ;D \$\endgroup\$
    – Vilmantas
    Aug 13, 2017 at 13:48
  • \$\begingroup\$ How are you handling bounces off of walls? In order to predict that a bounce will occur, we need to know how your game currently decides something should bounce (eg. are you using Collider2D objects, or do you have your own map data structure that you check for walls?) \$\endgroup\$
    – DMGregory
    Aug 13, 2017 at 13:49
  • \$\begingroup\$ I do use box colliders on walls and circle collider on pacman (2D) \$\endgroup\$
    – Vilmantas
    Aug 13, 2017 at 13:51

1 Answer 1


You don't have to use raycasting. The simplest solution would be adding rigidbody2D to your pacman, and then apply some force to this body with AddForce(...) method when pressing button. PS Set gravity scale of body to 0.

  • \$\begingroup\$ That would result in the same behaviour OP describes as a problem they want to solve: the character tries to move into the wall and bounces off of it due to restitution. \$\endgroup\$
    – DMGregory
    Aug 13, 2017 at 22:06
  • \$\begingroup\$ The question was "my question is how to prevent character from movement if there's a wall or it won't fit there". Such prevention can be done by using collider2D and rigidbody2D. \$\endgroup\$
    – Derek K
    Aug 15, 2017 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .