- Home /
Make object visible through selected objects only
I have an object, such as a gun, inside a cylindrical enclosure. The player can move around but since gun is inside cylinder, he can't see it. Upon activating a power, he gets something like X-Ray vision and can see the gun through the cylinder. This much I have been able to achieve using another camera with higher Depth, Culling Mask set to only the gun's layer and Clear Flags set to Depth Only.
Now I want that the object should be visible ONLY through the cylinder. Right now it shows on top of everything. If he has a wall or any other object in between him and the cylinder, the gun should not be visible. I cannot set the cylinder to transparent as the cylinder has its own texture and is opaque(wall and objects behind the cylinder shouldn't be visible through it) and the gun should have 100% opaqueness and visibility(so no partial transparency). I'm unable to find a way to do this. Don't mind using shaders if it's possible through them...
Answer by RoofTurkey · Mar 30, 2016 at 12:50 PM
Probably you can raycast from the player (camera) to the gun and check if it hits a cylinder, using a tag for the cylinder. If it does then enable the renderer on the gun, else don't.
This would look something like this:
Vector3 dir = gun.transform.position - camera.transform.position;
dir = dir.normalized;
if (Physics.Raycast(camera.transform.position, dir, out hit))
{
if (hit.tag == "Cylinder")
{
gun.GetComponent<Renderer>().enabled = true;
}
else
{
gun.GetComponent<Renderer>().enabled = false;
}
}
For more info check: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html http://answers.unity3d.com/questions/697830/how-to-calculate-direction-between-2-objects.html
Thanks for your idea. Your method comes very close to what I want. But it only checks if the center of the object is in view and shows all of it if the center is visible and hides all of it if the center is out of view. For example, If it's a large or long object(like the gun in our case) an object could be obstructing 1/3rd of the object but since the center of the gun is visible, the 1/3 part behind the object will show through it. That will ruin the effect I'm going for.
Good point. In that case I'd say you might wanna try putting all 'default' objects on one camera (that renders first/on top), then have the second 'gun' camera and finally have the 'transparant for guns' camera. That should give you the right effect, right? This way you should get the right effect on the objects in the third camera render and not on the other objects in the first camera render.
That's it! Thanks for your help! I can't believe it was that simple... Next I'll try having depth order among the cylinders. That is, have multiple cylinders such that the one nearer hides the cylinder behind it along with its gun. I'm thinking that might require a separate camera for each cylinder and setting their depths at real time according to a depth test for cylinders. $$anonymous$$ight become very resource intensive.... Thanks again for your help. I'll mark it as the answer and hopefully someone in the same boat will read your comment to figure out the answer...
Your answer

Follow this Question
Related Questions
i cant see the objects 2 Answers
make gameobjects visible when behind other gameobjects 3 Answers
How to make my brown circle transparent so i can see the green ground? 0 Answers
Object see-through when rendering mode is set to Fade 1 Answer
Standard shader's Fade rendering mode won't allow full opacity with a textured material 0 Answers