- Home /
How do you dynamicly change a cameras culling mask based on an object selection?
I'm not sure how to even ask this question. I have been working on it all day and reading forums scripting reference pages and answers on this site but I'm just not understanding.
I need to set the camera to only look at a certain layer culling mask based on a gameObject selection.
Something like this:
public var LayerName
//Make the code reusable when attached to different objects
//by changing the visible layer in the inspector
if(Input.GetMouseButton(1))//mouse over and left click a collider object
{
Camera.mySecondCam.cullingMask = LayerName;
//Make the second cameras culling mask change to a preset layer name or number.
}
The above code is not functional and is listed only for aid in explaining the end goal.
So I may need ray casting and I need to know what to code in the "layer name or number" section. I have looked into camera target swapping and using multiple cameras turning them off and on like light switches when parts of the main gameObject are selected. (each part has its own collider)
I have an object visible in the main camera and I have a second camera acting as a picture-in-picture view of selected object parts. The second camera is based on the MouseOrbitZoom camera found on the Unify Community Wiki. When parts of the main camera object are selected the second camera displays a close up copy (prefab) of the selected object.
For example if I had a car in the main cam I could select the door (layer 1 for example) and the second camera's culling mask layer should be set to that objects prefab layer (layer2) then I select the main cameras tire (Still layer 1) and the second camera needs to change to that objects prefab (Layer 3 for example)
I am not moving the camera so each object has a double that is set in a base location with the second camera targeted to view and rotate around any of the visible objects (Door or tire in this example). So long as I can change the culling mask layer based on different objects selected I should be able to accomplish my goal.
I hope that's clear enough to explain my issue. If it is not please feel free to ask for clarification.
Thanks in advance for any help.
This has already been answered: http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html
Thanks I had not found that article. However I dont feel that really answered my question. Perhaps I'm just to tired to understand it at the moment and I will have to read it again in the morning.
On a Side note I found Bob Berkebile's "Layer masks Simplified" Script and tried to use:
public class CameraOperator : $$anonymous$$onoBehaviour {
public float LayerNum = 0;
// Use this for initialization
void Awake () {
//Set a camera to only look at layer the set mask:
if (Input.Get$$anonymous$$ouseButton(1))//leftclick
{
camera.culling$$anonymous$$ask = Layer$$anonymous$$askHelper.OnlyIncluding( LayerNum );
}
}
}
but I'm still not having any luck.
Yeah I read over the link Lockstep graciously sent and again while it's very informative it does note address my issue. I know how to change the culling mask and I can manually set my cameras mask without any issue. I understand the bit shift. $$anonymous$$y issue is I don't know how to make the code look for the layer I want to show when an object in the sceene is clicked on.
Think of this as a split screen. The full car in one window and parts in the other. I click on parts of the full car and should get close up views in the second window. I can get one part to view but how do I change from one part to another? I have each object in a named layer I just need to be able to change the cameras layer culling based on clicking gameObjects. Am I doing this completly wrong? Am I missing something in Lockstep's post? I'm new to program$$anonymous$$g but bit shift dosnt seem to be what I need as I already have culling maskes created.
Answer by Giantbean · Feb 11, 2013 at 09:36 PM
OK I was being stupid and asked the question the wrong way. What I wanted to do does not require culling masks as I can simply change the visibility of the item like a light switch.
I used Javascript:
#pragma strict
var linkedMesh : Renderer;
function OnMouseDown()
{
linkedMesh.enabled = !linkedMesh.enabled;
}
Now I just need to add the code to make other objects visibility turn off when this code is in use. A few more "Renderer" variables and I should be set.
Edit: Its all working. I added more "var linkedMesh(01-12) : Renderer;" lines for each item and set the layers that I did not want visable to "linkedMesh.enabled = false". Its not exactly what I wanted but its working. I also used culling layers on the cameras in the sceene but I set them manualy.
Another way to do this If anyone is still viewing this old post.
Use tags and layers something like this:
//C# Unity class
bool isClicked = false;
GameObject baseObject;
string obj_name;
string layerName;
//on start code and such
void On$$anonymous$$ouseDown () {
obj_name = this.gameObject.name;
baseObject = GameObject.Find( obj_name );
string obj_tag = baseObject.gameObject.tag;
if (isClicked == false){
switch (obj_tag)
{
case "tag_tag1": // your tag name
Show ("tag1"); //your layer names
Hide ("tag2");
Hide ("tag3");
isClicked = true;
break;
case "tag_tag2": // your tag name
Hide ("tag1"); //your layer names
Show ("tag2");
Hide ("tag3");
isClicked = true;
break;
case "tag_tag3": // your tag name
Hide ("tag1"); //your layer names
Hide ("tag2");
Show ("tag3");
isClicked = true;
break;
default:
break;
}
else {
switch (obj_tag){
case "tag_tag1": // if a is a string
Hide ("tag1");
isClicked = false;
break;
case "tag_tag2": // if a is a string
Hide ("tag2");
isClicked = false;
break;
case "tag_tag3": // if a is a string
Hide ("tag3");
isClicked = false;
break;
default:
break;
}
}