Freeze physics body position

how can i freeze the physics body rotation and position? i want when the object collided, the object only can move to the left or right and freeze the z position.

not sure its doable to freeze specific axe using the world physics not using much codes.

But, you can make invisible walls to prevent object moving that direction
I did that trick in my basketball lens Линза FlipperBall от Jorik Rosa ― линзы и фильтры Snapchat