How to create invisible walls in a 2d environment

Hi guys

This is my first time making a game so forgive me if I’m asking really easy questions. I’ve put together the basics of a top down 2d scrolling game for mobile devices. I’de like the player to be able to move in the centre of the phone but the sides are going to have walls for a background so moving in those areas would look really weird. I’ve tried adding cubes with box colliders but the character just pushes them out of the way or goes straight through I’m sure I’m missing something in the code and would really appreciate some help with this one.


this is the code I’m using to move the character:

function Update()
		ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		if (Physics.Raycast (ray, rayCastHit))
			transform.position.x = rayCastHit.point.x;


I had the same problem when working in 3D. Are you moving your character with a rigidbody? If so, try to use alternate means for it, such as a standard character controller.

And you can use RayCasting with layersmasks.

I.e “int wallLayer = 1 << 9” declare the 9th layer in the layer list. Then you have to invert the mask to only trigger on the 9th layer:

wallLayer = ~wallLayer

Then in you use your rayCast with your wallLayer as your layermask


If i undertood what do you want, invisible walls, when your player hit them he stop right?

First you will need your player to have Colider2D, same as the walls

For a player to able to move those invisible walls, means that they have RigidBody

  • Set walls “Mass” to 999, so they can be very heavy
  • Set walls Gravity to 0, so they wont fall down :stuck_out_tongue:
  • Set player “Mass” to 0.01, so he cant push nothing, he will be very light
  • Set walls Gravity to 0, so he wont fall down
  • To turn them invisible just remove Sprite Renderer
  • To give them Colider Shape, you can use BoxCollider2D and change size, you will see some green line :wink:

I hope i helped from what i undertood, good luck have a nice day