Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zitoox · Sep 10, 2016 at 08:07 PM · cameraflashcreatehow-tofor

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sergio7888 · Sep 10, 2016 at 11:46 PM 0
Share

use a Light, enable during the flash duration and disable the light.

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zitoox · Sep 11, 2016 at 06:00 PM 0
Share

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.

avatar image breban1 Zitoox · Sep 11, 2016 at 06:10 PM 0
Share

@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.

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zitoox · Sep 11, 2016 at 06:06 PM 0
Share

@iFallOffStuff

There are lots of errors on it, i can't even describe... =\

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

75 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

(C#) How to capture video stream without using a third party SDK 2 Answers

Screen Flash Red when told 1 Answer

Access to camera flash 0 Answers

Does Flash Export support "Depth Only" camera clear? 2 Answers

How to move the camera depending on the score? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges