conditionaly transparent layers in 2D shooter

Hi Guys,

Ive just started working on a simple 2d top down shooter in Unity 4.3 using 2D mode and perspective camera. How it works now is that I have 3 layers :
-floor - upon which the player / mobs walk,
-entitites - on which I render all the entities like players, enemies etc
-ceiling - where I have things like tree tops or some hanging stuff like changeliers etc. with which player cant interact.

So Im trying to figure out a good way to make those sprites on the ceiling layer have like 50% opacity whenever the player is underneath them so that the player can be seen always.

Any ideas how to implement that ?

I answered a similar question here:

The game stores all transparent objects in a list and makes new additions to this list transparent and objects that leave the list, because the raycast doesn’t hit them anymore, opaque again.

Cast a ray from the camera to the player. If it hits an object on your ceiling layer, make the object semi-transparent and set a timer for something like 1 second. When your timer runs out, make the object opaque again. This will mean that while your player is beneath the object, the ray will keep hitting it each frame and will keep resetting the timer, but once the player moves away the object will stop getting hit, the timer will then run down and the object will become opaque again.

You might be able to get a nice effect by making the opacity inversly proportional to the remaining time, so that objects fade back in when they no longer obscure the player.

Another possibility is to give objects on the ceiling layer colliders set to be triggers. Make them transparent in the OnTriggerEnter function, and change it back in the OnTriggerExit function. If the player is the only thing you need to see under them, then alter the layer collision matrix so the player is the only collision detected. Otherwise, you’ll need to check the name of the colliding object to make sure it’s the player, and maybe keep count of events in case 2 or more things go under a ceiling object at once.