2D Game, Player Not to move if a wall is next to it C#

Hello There.

I am very new to Unity, and i am making a 2D game with no gravity physics using C#.

Everything in the game is a blocky 32x32 gameobjects.
The player moves from block to block (“grid to grid”), so that is 32 pixels between each GameObject.
I want the player to not move in a direction(left, right, up and down), if there is a wall on the grid the player is trying to move to.

In more pseudo code i think i want something like

if ( There is NO GameObject.WithTag(“Wall”) 32px left of player current position)

move 32px to the left(to next block/grid)


do nothing

Thanks in advance for the help

You wouldn’t want to do tag checking for this because it would happen too frequently and be a bit slow.

It sounds like you need basic collision detection. To do this you can build your character controller to use the built in physics, with colliders and rigidbodies, or you could do a custom solution, where you keep some sort of array of the level “blocks” and check if the spot your player will be next frame according to the position and velocity - and if the spot is currently occupied with a “block”, then deflect the x velocity of your character controller.

You can find many many tutorials on setting up your own collision system, or using the built in stuff that comes with unity with a quick google search.

Good luck!