In certain games, there is a function involved where if there is an object between the camera's view and the player, the object will either become transparent, or the player's silhouette is visible on the object. Would I need some sort of custom diffuse shader, or is there a function that will allow me to change the visibility of an object.
Also, how would I do a raycast/vector check to determine if the object is between the player and camera.
Would I need Pro for the transparency part?
asked Jan 27, 2011 at 05:48 AM
Just do it like Jean said. The Unity script reference on RaycastAll gives you an example for exact your case except it does not revert the transparency. A list would be a solution, but it gets tricky to determine what renderers have to be reverted, which ones should stay transparent and which ones have to set transparent.
I would write a little script that you can attach dynamically to those renderes which should be transparent. In that script you can implement some kind of falloff and when it'sno longer needed it removes itself.
Here's an example script that gets dynamically attached to the renderer and the script that do the raycast.
//And here the script for the camera to cast the ray / capsule ;
The two script files need to be named like the class names (AutoTransparent and ClearSight).
edit I guess your camera script is also on the camera like my ClearSight script.
But don't forget: you need the distance in units. If you have the distance already, great, just pass it to my script ;)
Raycasting would be a start, that is, you raycast from the player position and whatever is hit before the player should be send a message or dealt in some way to become semi transparent. you would need to maintain a list so that you can toggle semi transparent models back when not anymore in front of the player,
Else, you could actually have a box collider starting from the player and pointing at the camera where its size emcopass the player bounds, then anything that collides to that are also in the way.
Otherwise, Physics.CapsuleCast could be also an alternative to building a collider, but very similar in principle, tho the start point would need to be sligtly tweacked and not start right where the player is but slightly closer to the camera to not hit the ground or things to close to the player.
As for the shader, I would replace it with a specific simple one while in front, you might want to have front and backface material for object very close to the camera.
Hope it helps,
Here's how you can use it:
Hope somebody finds this useful.
answered Apr 03, 2011 at 08:17 PM
I have a variant that works with Triggers. I have a question: is CapsuleCastAll less expensive than using a Trigger attached to the camera?
TransparencyControl is attached to the object being made transparent/opaque. The object has to have the RigidBody attached to it.
The Trigger is just a box collider aligned between the Main Camera and the Target Cube. I'm thinking of creating a pyramid, scaling it to the view frustum and using that as the Mesh Object for the Trigger. Not sure whether that will be more expensive or not ...
answered Aug 28, 2013 at 08:23 PM