How to actually make raycast hit colliders it s starting from?

Hi everyone!

I have a very good knowledge of how raycasts work, with the layers and stuff, but today I have a problem that apparently nobody has, or at least they are overwhelmed by the opposite problem so I couldn’t find a solution:

I am making kind of an RTS with a non-rotating isometric camera, thus, when units go behind walls, I want to render them in transparency, like this:

So as you can see, it works fine by now, the only issue is that to do this, I do a capsuleCast from my character to the camera, but as I can go inside buildings, I am currently forced to use a mesh collider on my building or manually place box colliders on all my walls to detect them when I’m inside. Primarily I wanted to simply use a box collider all around the building to optimize the collisions, but of course, if I am inside, the capsuleCast doesn’t detect the collision.

Here’s a little illustration:

So my question is this: is there a way to actually detect colliders the raycast is done from, please?
(I saw something about changing a “Raycast collision inside” parameter or so in Edit->Preferences->Physics, but I couldn’t find the parameter)

Also, I’m using Unity 5.2

Thanks a lot for your time!

Can’t you do the CapsuleCast from the Camera to the Player instead of from the Player to the Camera? The Camera isn’t inside a BoxCollider and so it can get the detection on the BoxCollider the player is inside.

Eg… instead of

if (CapsuleCast(fromPlayer, toCamera)) ...

do

if (CapsuleCast(fromCamera, toPlayer)) ...