How to get a sprite to draw on top of the background but not over the player?

I have a scene with a 3rd person character where the camera floats behind the player. When the player walks through the world, I have sprites appear over objects that the player can interact with.

I have three cameras in the scene: 1 orthographic camera for the GUI, and 2 perspective cameras (one for the game, and another camera that only renders the 3d sprites) The game cam has the lowest depth, followed by the 3d GUI Cam, and then the orthographic GUI Cam on top.

The problem I'm running into is that I don't want to see the selector sprites through the Player character. I want them to appear through other objects, just not the player.

Currently I have it set up where the sprites are toggling themselves on or off by linecasting to the camera and then checking if it hit the player collider. It works for the most part but i'm sure there has to be a better way of doing it? A shader perhaps? I'm still very much a shader newbie so I have no idea...

With shaders - here is some basic idea to start from.

  • Draw scene objects first.
  • Draw 3D GUI objects, on top of everything, without writing to z buffer.
  • Draw player.

Scene shaders: (no need for change, they are probably already in Geometry queue)

Tags { "Queue" = "Geometry" }

3D GUI shaders:

Tags { "Queue" = "Geometry+1" }
Zwrite Off
Ztest Always

Notice that GUI object more close to camera can appear under GUI object that is farther.

Player shaders:

Tags { "Queue" = "Geometry+2" }

This is simple case, when there are no transparent object in scene etc. But it’s a concept that can be further refined.