Limiting player movement to only two directions at a time

Hello. I have a shape made out of parts (the shape is generated automatically around walls) and want to make players able to move only along the lines. I want players to be able to rotate their cameras and press all movement keys (WASD, not only A and D or W and S) and still walk only in one of the two possible directions at a given time.

I don’t know where to start. I thought about a couple of methods but none of them are viable.

I am making this so players will be able to walk next to walls while hiding behind them.