For example, in Five Nights at Freddy’s 3, you can hallucinate and after hallucinations your vision starts blacking out, then the black fades, then comes back. How can I do something like this?
If You want something simpler just add proper Image to Your canvas so it overlays screen) and then manipulate it with:
MyImage.CrossFadeColor(NewColor, fadeDuration, false);
MyImage.CrossFadeAlpha(NewAlpha, fadeDuration, false);
You can use it to add bloody-screen effect or many others by just mixing textures.
This have big advantage: You call it once and don’t have to worry to lerp between values in every frame.
A lot of those effects are done with post processing shaders (which are now available free to everyone). If you haven’t used shaders these are pretty easy to get. The Unity Manual has a great image effects reference ans sample set here and I adapted this tutorial to make a sample for you.
In total you will need a controller script, blackout shader, and material to hold the shader. The controller script will attach to the camera and use a float (“rate”) to set shader properties of the assigned material.
using UnityEngine;
using System.Collections;
public class Blackout : MonoBehaviour
{
public Material mat;
[Range(0f,1f)]
public float rate = 1f; //0 is black, 1 is original color
void Update()
{
mat.SetFloat("_Rate", rate);
}
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit (source, destination, mat);
}
}
All of the good stuff happens in the fragment shader. In this case all we really want to do is adjust the original pixel color by a percentage rate where 0 = full black and 1 = original lit color.
Shader "Custom/Blackout"
{
Properties {
_MainTex ("", 2D) = "white" {}
_Rate("Amt original color", Range(0.0,1.0)) = 1.0
}
SubShader {
ZTest Always Cull Off ZWrite Off Fog { Mode Off }
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float _Rate;
struct v2f
{
float4 pos : POSITION;
half2 uv : TEXCOORD0;
};
//Vertex Shader
v2f vert (appdata_img v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord.xy);
return o;
}
//Fragment Shader
fixed4 frag (v2f i) : COLOR
{
fixed4 orgCol = tex2D(_MainTex, i.uv);
fixed4 col = orgCol * _Rate;
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
With this done pop into the editor and create a new material. Assign the blackout shader to it and assign the material to script on the camera. When you run the scene you should be able to use the “Rate” slider to interactively adjust the darkness on the screen. From here you could easily make an automatic blackout using time (rate = Mathf.Sin (Time.time);
), but you’ll probably want to push it further
I would recommend looking into related shaders such as the sepia effect, color inversion, color channel adjustment, and texture multiply/overlay which are all simple effects that extend off of this. Leaving this open rather than attaching files so people can easily see/make suggestions as I am a newbie at shaders.