- Home /
How to make a FLASH
I was trying to make a video camera in a test project and it's almost done. I have the camera prefab, i have the viewer, i have the "take a photo" script, but i need a last thing, and i don't know how to make it.
Most cameras make a white flash when they take a photo. How can i do this in a game? Like, how do i create a flash particle or something and apply it to the game? Like a shot, but a flash instead.
use a Light, enable during the flash duration and disable the light.
Answer by breban1 · Sep 11, 2016 at 02:07 AM
1) Create a c# script named Flash, copy & paste this script into it.
2) Create a white UI Image (GameObject->UI->Image), pick a Source Image for the image in the inspector (I used a 4x4 white pixel). Set the RecTransform to stretch, 0, 0, 0, 0 (left/top/right/bottom).
3) Attach the Flash script to your UI Image object.
This was quick and dirty, but I think it works well.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Flash : MonoBehaviour {
///////////////////////////////////////////////////
public float flashTimelength = .2f;
public bool doCameraFlash = false;
///////////////////////////////////////////////////
private Image flashImage;
private float startTime;
private bool flashing = false;
///////////////////////////////////////////////////
void Start()
{
flashImage = GetComponent<Image>();
Color col = flashImage.color;
col.a = 0.0f;
flashImage.color = col;
}
///////////////////////////////////////////////////
void Update () {
if(doCameraFlash && !flashing)
{
CameraFlash();
}
else
{
doCameraFlash = false;
}
}
///////////////////////////////////////////////////
public void CameraFlash()
{
// initial color
Color col = flashImage.color;
// start time to fade over time
startTime = Time.time;
// so we can flash again
doCameraFlash = false;
// start it as alpha = 1.0 (opaque)
col.a = 1.0f;
// flash image start color
flashImage.color = col;
// flag we are flashing so user can't do 2 of them
flashing = true;
StartCoroutine(FlashCoroutine());
}
///////////////////////////////////////////////////
IEnumerator FlashCoroutine()
{
bool done = false;
while(!done)
{
float perc;
Color col = flashImage.color;
perc = Time.time - startTime;
perc = perc / flashTimelength;
if(perc > 1.0f)
{
perc = 1.0f;
done = true;
}
col.a = Mathf.Lerp(1.0f, 0.0f, perc);
flashImage.color = col;
flashing = true;
yield return null;
}
flashing = false;
yield break;
}
}
After that, call CameraFlash() to flash the image. You can test in editor by pressing the "Do Camera Flash" checkbox.
I didn't get it. What was it supposed to do? I followed the instructions and the image just stay there without flashing or something.
@zitoox I have this exact script set up in my scene and it works. Are you pressing the Do Camera Flash checkbox in the script when it's running in the editor?
If you can't get it to work I can try stepping through it with you. $$anonymous$$essage me if it's still not working.
Answer by iFallOffStuff · Sep 11, 2016 at 02:06 AM
It's a relatively simple script actually, this is some code almost identical to code in the Survival Shooter tutorial:
public Image flashImage;
public float flashSpeed = 5f;
//Time the flash lasts for
public Color flashColour = new Color(1f, 1f, 1f, 1f);
// The values above correspond to: R - G - B - Alpha these values would produce an opaque white flash.
void Update ()
{
if(picture)
{
pictureImage.color = flashColour;
}
else
{
pictureImage.color = Color.Lerp (pictureImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
picture = false;
You will then simply need to assign the script and add a 'flash image' in the inspector view. Let me know if you need any more help or if i'm generally confusing!
Answer by iFallOffStuff · Sep 11, 2016 at 02:06 AM
If I understand you correctly, you want a white flash to appear when you take a picture? The code would look something like this:
public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
//The values above are in the order of R - G - B - Alpha
public float flashSpeed = 5f;
//The time the flash will last for
void Update ()
{
if(take.picture)
{
flashImage.color = flashColour;
}
else
{
flashImage.color = Color.Lerp (flashImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
flash = false;
I think that should work, please let me know if it doesn't! You'll also need to assign a few things in the inspector (flash image)
There are lots of errors on it, i can't even describe... =\
Answer by Grish · Sep 11, 2016 at 02:08 AM
Hi,
If the flash is only going to affect the player, you can add a canvas with a white image. Then quickly reduce the alpha of the image and after a few seconds destroy or disable the canvas.