- Home /
How to set alpha for object "hidden" by another?
I have a game in which the camera is in front of, and always facing the object and moving along with the object (similar to a 2D scroller, but in this case it is 3/4-down aerial 3D view)... since there are (a few) areas of the level where the player could pass behind another object and be hidden from sight, I'd like to render the player mesh at 50% alpha.
What is the best way to do this? I have searched Answers and Forums and found bits and pieces, but still having problems. My scenario is this:
1) I have 2 cameras for rendering different layers; one uses culling to render the level/background, the other renders the player object (so, in fact, right now when if you fall "behind" something, you are actually rendered in front of it -- even more confusing!)
2) The player object includes multiple children/grandchildren (including a multi-mesh model and a point light); these meshes all have Transparent diffuse shaders attached to allow setting alpha levels
I am attempting to do a Raycast to check whether something is in front of (hiding) the player, and set alpha accordingly, but it's not working...
function Update () {
var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
if(Physics.Raycast (cam.position, cam.forward, hit, 100)) {
if (hit.collider != currentPlayer.collider) currentPlayer.renderer.material.color.a = 0.5; // if player not visible, change alpha
}
else currentPlayer.renderer.material.color.a = 1.0;
}
For one thing... I assume doing this doesn't work on the player object's children -- what is the best way to call a child or set of children (I could tag them if that helps); I don't need to render the whole game object, but the main model mesh which is a child of the collider object.
I'm not pretty sure, but i think, you may do something like you want, with combination of Depth$$anonymous$$ask and Stencil buffer.
Hmmm... maybe. I will have to read up on this. At first glance, the examples seem to be doing the opposite of what I want ie. NOT rendering something when it is actually in front; whereas I want TO render something, when it is actually behind ... but moreso, I want to render it differently, with a 50% see-through to give an "x-ray" effect through the blocking mesh and onto the player mesh; even though in actually I am rendering the player in front of the obstruction, it would still have this effect... But I will need to research more about Depth$$anonymous$$ask and/or Stencil. They are more complex than any of the rendering I have done so far...
okay, after some more web searching I have found this, which may prove useful: http://chrisrolfs.com/game-dev/unity3d/hide-objects-blocking-player-view
And also this, which may just do the trick for me (I use the Unify wiki but guess didn't look in the right place for this solution -- didn't think it would be a special shader to do it. $$anonymous$$an, I really need to learn more about customizing shaders...)