Camera Culling Mask Change
Hi guys,
I am pretty new to Unity and have a question. I have 2 scripts that are basically 2 flashlights.
First is a normal flashlight, and the second one is a UV light flashlight. (Same thing, but a different color to the light).
My problem is the following: I have created a new layer called UVlight (Layer 10).
I want the camera (PlayerCamera) to render everything, besides the UVlight layer. When the player turns his UVFlashlight on, the objects tagged with UVlight layer to become visible if the player shines the light over them.
Here is the flashlight script: using UnityEngine; using System.Collections;
public class flashLight : MonoBehaviour {
private Light fL;
void Start()
{
fL = gameObject.GetComponent< Light >();
}
void Update()
{
if( Input.GetKeyDown( KeyCode.F ) )
{
if( fL != null) fL.enabled = !fL.enabled;
}
}
}
UV Flashlight Script:
using UnityEngine;
using System.Collections;
public class flashLightUV : MonoBehaviour {
private Light fLUV;
void Start()
{
fLUV = gameObject.GetComponent< Light >();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.U))
{
if (fLUV != null)
{
fLUV.enabled = !fLUV.enabled;
}
}
}
}
Any ideas on how to do this? I think it is a simple thing to do, but like I said, I'm kinda newbie at this and seek help from the more experienced devs.
Thanks in advance, Vlad
Answer by Zynek · Oct 28, 2016 at 02:46 PM
Well a naive solution to this might be simply changing culling mask at runtime. For that you would just add something like this into your UV Flashlight script:
//decleration
[SerializeField] private LayerMask normalCMask;
[SerializeField] private LayerMask uvCMask;
//On UV light Turn on
...
Camera.main.cullingMask = uvCMask;
...
And you would setup culling masks in the inspector. However more sofisticated solution would be to write your own shader, so the objects would be visible only if they are directly lit. There i cannot assist, but here is a tutorial on how to write shaders: https://youtu.be/epixwRw80MM
I tried this, but for some reason, it did not work. It did not change the cullingmask on the camera. Does it have to be the $$anonymous$$ainCamera? I use an Untagged camera, since this is the requirement of AdventureCreator.
The camera has to be tagget as "$$anonymous$$ainCamera" if you want to acces it through Camera.main.
But you can use this on any camera, you just need the right reference.
Answer by Landerk · Oct 28, 2016 at 03:58 PM
You could have a camera set to render the UV layer, place it above your main camera so it renders everything. Disable/enable it with the flashlight script.
Thanks for the suggestion. I used Zynek's method and i managed to get things to work as intended.
Answer by VisualPath · Oct 29, 2016 at 09:03 AM
Ok, so I'm dumb. I managed to make the Culling Mask change, but now I have a different problem. For the objects set on the UV Layer, I want them to be visible only when the UVFlashlight shines over them. And for example if the light shines only on one part of the object, I only want that part to be visible, and not the entire object.
Is there any way i could do that?
As i said, that requiers advanced aproach and most likely writing your own shader, but you can check out shaders at asset store, if anyone already made what youre looking for.
Ok, i managed to change the way i use the light and everything looks good now, and works as intended. Thanks for your help!