Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by kaelan · Nov 20, 2010 at 02:04 PM · lightinglightflicker

How to make a light flicker

is there a code i can use to make a light flicker on and off quickly

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by duck · Nov 20, 2010 at 04:21 PM

Try this script. Place it on the light gameobject.

It has two public variables that you can set in the inspector, timeOn and timeOff. They allow you to specify different amounts of time for the light to be on and off. The default is 1/10th of a second on, followed by half a second off.

var timeOn = 0.1; var timeOff = 0.5; private var changeTime = 0;

void Update() { if (Time.time > changeTime) { light.enabled = !light.enabled; if (light.enabled) { changeTime = Time.time + timeOn; } else { changeTime = Time.time + timeOff; } } }

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 Mark 6 · Dec 17, 2010 at 09:55 PM 0
Share

I had to change Void Update to Function Update to get it to work in js var timeOn = 0.1; var timeOff = 0.5; private var changeTime = 0;

function Update() { if (Time.time > changeTime) { light.enabled = !light.enabled; if (light.enabled) { changeTime = Time.time + timeOn; } else { changeTime = Time.time + timeOff; } } }

avatar image
0

Answer by Uriel_96 · Nov 20, 2010 at 04:33 PM

try this one if you want to make something like an eye to close and open it: http://unity3d.com/support/documentation/ScriptReference/Light-spotAngle.html put the minangle at 0 and the maxangle how you want and also the interval make it faster or slower.

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 xmediagrafx · Nov 08, 2011 at 06:25 PM

i use this....applied to your directional light.

function Update () 
    {
        if ( Random.value > 0.9 ) //a random chance
        {
           if ( light.enabled == true ) //if the light is on...
           {
             light.enabled = false; //turn it off
           }
           else
           {
             light.enabled = true; //turn it on
           }
        }
    }

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 ReflexGames · Sep 09, 2013 at 05:26 AM

Like this

var flickerSpeed : float = 0.1

while (true){

Light.enabled = true; Yield WaitForSeconds (flickerSpeed); } Light.enabled = false; Yield WaitForSeconds (flickerSpeed);

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 RasteRayzeR · Mar 24, 2014 at 06:05 PM

If you want something you can tweak a bit more, maybe you can try this solution I came up with to make a cheap lightning effect. You can tweak the flickering level by changing the size of the array (smaller means more flickering, larger means a smoother variation). This may not be optimal but does a fine job.

 using UnityEngine;
 using System.Collections;
 
 public class Ambiance : MonoBehaviour {
 
     // Your light gameObject here.
     public Light light;
 
     // Array of random values for the intensity.
     private float[] smoothing = new float[20];
 
     void Start(){
         // Initialize the array.
         for(int i = 0 ; i < smoothing.Length ; i++){
             smoothing[i] = .0f;
         }
     }
 
     void Update () {
         float sum = .0f;
         
         // Shift values in the table so that the new one is at the
         // end and the older one is deleted.
         for(int i = 1 ; i < smoothing.Length ; i++)
         {
             smoothing[i-1] = smoothing[i];
             sum+= smoothing[i-1];
         }
 
         // Add the new value at the end of the array.
         smoothing[smoothing.Length -1] = Random.value;
         sum+= smoothing[smoothing.Length -1];
 
         // Compute the average of the array and assign it to the
         // light intensity.
         light.intensity = sum / smoothing.Length;
     }
 }
Comment
Add comment · Show 3 · 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 DJ_Brem · Aug 02, 2015 at 09:25 AM 0
Share

A simple and effective solution. Works wonderfully and the effect is quite perfect for the burning torch that I am using it for.

However, I had an issue with your script not finding the Light-component when I dragged it in using the Editor.

It worked when I initialized the light-variabile in the Awake-$$anonymous$$ethod using GetComponent()

avatar image Jackson123 · Jan 07, 2017 at 06:02 AM 0
Share

How can i edit this so that it doesn't stop at around .5, because that is just a little to dark for me.

avatar image burly87 Jackson123 · Apr 13, 2018 at 10:21 AM 0
Share

You can set a Random.Range($$anonymous$$, max); to get the floats between thos two values.

example smoothing[smoothing.Length -1] = Random.Range(.5f, 20.0f);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I can't use 2D lights 2 Answers

How to I update multiple lights at once? 1 Answer

Spotlight shines out of range 1 Answer

Exclude certain objects from getting lit 2 Answers

How do I change a spot light to a point light by pressing a key? 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