- Home /
Best way to have a camera only see certain objects?
Layers aren't exactly what I'm looking for in this scenario. Basically, I want two (or more) cameras to be able to move across a timeline and render objects if and only if they exist at that point in the timeline. Time travel shenanigans.
Example Timeline:
<--WWWWW-------OOOOO-------------->
As the camera moves back and forth across the timeline (via player input) it would only see the Ws when at a point when they exist. Same for the Os. Naturally, there would be more than two objects and overlaps will certainly occur.
Is there a way to, on the camera, grab a variable from the objects and only render it if that variable matches? The only solution I found was to iterate through every object and enable/disable the rendering of it depending on where the camera is and that feels really clunky when done for multiple cameras in succession.
You said you were iterating through the objects and enabling/disabling rendering. Were you using OnPreRender()? You could set each object's color to Color.clear in OnPreRender(), and then set it back to Color.white (or whatever) in OnPostRender().
Answer by Cherno · Oct 19, 2015 at 09:56 PM
As Chuckleluck wrote, you can use OnPreRender and OnPostRender. In this example, the layer of an object is switched so it isn't rendered by this particular camera.
using UnityEngine;
using System.Collections;
public class SwitchLayer : MonoBehaviour {
public GameObject go;
public string layerName_0 = "MyLayer0";//the layer that is not rendered by this camera
public string layerName_0 = "MyLayer1";//The object's normal layer
void OnPreRender() {
if(go== null) {
return;
}
go.layer = LayerMask.NameToLayer("MyLayer0");
}
void OnPostCull() {
if(go== null) {
return;
}
go.layer = LayerMask.NameToLayer("MyLayer1");
}
}
Your answer
Follow this Question
Related Questions
Passing Underneath Section of a Mesh 0 Answers
BUGS Unity? I can't see my object if i build 0 Answers
rgb value detail 0 Answers
Is it possible to view two camera at the same time? 2 Answers