Occlusion masking material affecting background

I’m currently getting into materials and started work on a feature that’s masking out objects if they’re in between my character and my camera. I do this by comparing the distance between the pixels and my camera with the distance between my character and the camera. If the pixel is closer than the character and in a certain radius around it, they’re getting masked out. Now I’ve run into a few issues with this method. When applying the mask to a floor, or while standing in a corner, a big chunk of the objects are being masked out since they’re closer to the camera, although they aren’t really blocking the view.

Any ideas for how I could filter these pixels out without just turning off the effect on the problematic objects? Here’s how my Material Function is currently set up: