- Home /
How to render gameobject only if in certain portion of view rect?
How would I go about rendering a game object (3d model in this case) but only if it is in a certain area of the screen (eg middle 30% of screen).
I want to have something render only if its center is withing the bounds of a the screen middle lets say my screen is 480pixels height (example but need it to be resolution independent) and i want it to show only if its position is between(screen height) of 168 and 312.
Thanks for any help.
Answer by adrenak · Sep 06, 2012 at 08:08 PM
Have a look at `Camera.WorldToScreenPoint` here :
http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html
the sample code pretty much explains everything
I Hope this helps!
Thanks I tried that but I decided to go with its "cousin" Camera.WordToViewPort
Answer by PAEvenson · Sep 06, 2012 at 08:51 PM
You want to use 2 cameras.
Set each camera's clear flag to depth only
set each camera's depth(the higher depths draw on top)
Create a new layer
set the gameObject's layer to the newly created layer
change the "clipping" camera's Cull Mask to only your new layer
Set the other camera's Cull Mask to everything but your new layer
Adjust the normalized view port rect to the appropriate size
Your gameobject will only be visible when inside the view rect.
Cant do that because I am using an AR solution but thanks.