How to make character stop when facing the wall

I tried doing something like that, but the trace is infinite and only appears when I press forward.