Restrict player movement on axes (Y or others), like 2D games?

It is possible to restrict the player’s movement to an axis, I want to make a 2D game but I want to block the inputs that take it downwards and forwards, moving only sideways (A, D, right arrow, left arrow), since that we have the fixed angle camera?

Easiest way I can think of is to just have barriers blocking the characters’ movement on each side