How to turn everything black except from certain objects in the scene in a first person camera view
Hi,
I'm trying to create a script that in a certain moment all of the screen turn black but from my characters. The only problem is that I would like to do that only from the camera's first person look. For example if one of the characters is inside of a house - I won't be able to see it. I tried changing the camera's setting and changing the character's layers but I could see the characters that were inside buildings and behind objects and I wouldn't want that..
Any ideas pleas?
Answer by UnityCoach · Jul 28, 2017 at 10:15 PM
ReplacementShader is a bit restrictive for what you intend to do.
Here's what you can do instead.
First, you need a "Manager" script to handle the toggle.
[AddComponentMenu ("UnityCoach/Answers/MaterialSwapManager")]
public class MaterialSwapManager : MonoBehaviour
{
[SerializeField] KeyCode switchKey = KeyCode.C;
public delegate void MaterialSwapEvent (bool state);
public event MaterialSwapEvent OnMaterialSwap;
private bool _state;
public bool State
{
get { return _state; }
private set
{
if (value != _state)
{
_state = value;
if (OnMaterialSwap != null)
OnMaterialSwap (_state);
}
}
}
private Rect _screenRect;
void Awake ()
{
_screenRect = new Rect (0, 0, Screen.width, Screen.height);
}
void Update ()
{
if (Input.GetKeyUp (switchKey))
SwapMaterials ();
}
void OnGUI ()
{
GUILayout.BeginArea (_screenRect);
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
GUILayout.Label (string.Format ("Press {0} to Swap Materials", switchKey));
GUILayout.EndVertical();
GUILayout.EndArea();
}
void SwapMaterials ()
{
State = !State;
}
}
This script will simply take an input key, you can replace this with anything else. It'll handle a bool "state" and raise an event when the value changes.
Then, you need another script on every object you want to swap materials on.
[AddComponentMenu ("UnityCoach/Answers/MaterialSwap")]
[RequireComponent (typeof (Renderer))]
public class MaterialSwap : MonoBehaviour
{
[SerializeField] Material replacementMaterial;
private Material [] originalMaterials;
private Material [] replacementMaterials;
private Renderer renderer;
void Awake ()
{
renderer = GetComponent<Renderer>();
originalMaterials = renderer.materials;
replacementMaterials = new Material[] {replacementMaterial};
MaterialSwapManager mSwapMgr = FindObjectOfType<MaterialSwapManager>();
if (mSwapMgr)
mSwapMgr.OnMaterialSwap += OnMaterialSwap;
}
void OnMaterialSwap (bool state)
{
renderer.materials = state ? replacementMaterials : originalMaterials;
}
}
This script will subscribe to the Manager (one has to be in the scene), then swap the materials on the objects. You can simply assign a Black material to everything but the characters.
SetReplacementShader would be pretty powerful to add things like night vision and other effects, but it really forces you to manage every shader you'll use in the project. Something you may want to have a look at in the future.
Your answer
Follow this Question
Related Questions
Instantiate screenshots from camera *Realtime* 0 Answers
RigidBody First Person Controller Not Working 0 Answers
Sit Down First person 0 Answers
Render to minimap on command 1 Answer
Third Person Shooter Controller 0 Answers