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
5
Question by Muhasaresa · Jan 08, 2011 at 08:56 PM · randomlightintensity

How to Randomly Change the Intensity of a Point Light with a Script

I am creating a fire and I made it emit some light using a Point Light. How can I randomly change the intensity of the Point Light using a script?

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

4 Replies

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

Answer by Statement · Jan 08, 2011 at 11:13 PM

I cooked you a script that looks rather nice. Just add it to a light and adjust min and max intensity to suit your flavor. The random part is there so that different lights behave slightly differently. Otherwise they would all flicker in sync. You can probably also do something similar with your lights range, if you want to as well.

It works by sampling perlin noise which is gradient with respect to neighboring values. I then use Time.time to step through these values smoothly over time. I add a random value to avoid repetition among lights. The script only changes the attached lights intensity between minIntensity and maxIntensity. If you want to change how rapidly the flicker changes then you can multiply Time.time with some value, as that will cycle through the set much faster. Since PerlinNoise return noise in the interval 0 to 1, we can use this with linear interpolation to flicker between two set values.

 using UnityEngine;
 
 [RequireComponent(typeof(Light))]
 public class SoftFlicker : MonoBehaviour
 {
     public float minIntensity = 0.25f;
     public float maxIntensity = 0.5f;
 
     float random;
 
     void Start()
     {
         random = Random.Range(0.0f, 65535.0f);
     }
 
     void Update()
     {
         float noise = Mathf.PerlinNoise(random, Time.time);
         light.intensity = Mathf.Lerp(minIntensity, maxIntensity, noise);
     }
 }


Here's the JS version if you rather prefer that.

 @script RequireComponent(Light)
 
 var minIntensity = 0.25f;
 var maxIntensity = 0.5f;
 
 private var random : float;  
 
 random = Random.Range(0.0f, 65535.0f);
 
 function Update()
 {
     var noise = Mathf.PerlinNoise(random, Time.time);
     light.intensity = Mathf.Lerp(minIntensity, maxIntensity, noise);
 }

             


Comment
Add comment · Show 4 · 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 PrimeDerektive · Jan 09, 2011 at 12:20 AM 2
Share

Useful stuff there!

avatar image Muhasaresa · Jan 09, 2011 at 11:30 AM 2
Share

Thank you so much for the scripts :-D

avatar image DillonRobinson · Jun 17, 2014 at 02:31 AM 1
Share

Glorious, thank you, works perfectly

avatar image JanEricsson · Apr 01, 2015 at 05:00 PM 1
Share

Thank you! <3

avatar image
1

Answer by Jesse Anders · Jan 08, 2011 at 09:06 PM

I haven't done much scripting with Unity's light components, but I'd guess this could be accomplished by modifying properties of the light component (e.g. color, intensity, range) via scripting in real time.

The 'intensity' parameter might be a good place to start. Something that would be fairly easy to try would be simply to assign the value of Random.value to the 'intensity' field/property (with the range of the value adjusted appropriately).

To get a more realistic effect though, you may want to apply some sort of smoothing or hysteresis, or use a 'less random' method of generating the random input.

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 Statement · Jan 08, 2011 at 11:20 PM 0
Share

The smoothing you talk about can be easily achieved with $$anonymous$$athf.PerlinNoise

avatar image
1

Answer by Noise crime · Jan 08, 2011 at 10:32 PM

Personally i'd use an animation for this and not a script. It will be far easier to control, tweak and obtain the effect you are after. Light.Intensity as well as other light properties are all exposed in the animation editor so its simple to generate the animation.

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 Statement · Jan 08, 2011 at 11:21 PM 0
Share

The problem with animations is that they loop. This doesn't look nice when looking at a fire for a long time.

avatar image Noise crime · Jan 09, 2011 at 12:07 AM 0
Share

Then you just create an arbitrarily long animation, besides how long do you stare at a fire? I seriously doubt anyone would notice it repeating if you made it a decent length (e.g. 10 seconds). But your script is a nice alternative.

avatar image Statement · Jan 09, 2011 at 02:12 PM 0
Share

I am not saying it's a no no. I just brought up a problem with it, worth considering. I actually upvoted you and Jesses answers. I think both contribute to the better.

avatar image
0

Answer by Antigen_Z · Jul 12, 2015 at 09:41 PM

 using System;
 using UnityEngine;
 using Random = UnityEngine.Random;
 
 [RequireComponent(typeof (Light))]
 public class TorchLight : MonoBehaviour {
 
     public float MinLightIntensity = 0.6F;
     public float MaxLightIntensity = 1.0F;
 
     public float AccelerateTime = 0.15f;
 
     private float _targetIntensity = 1.0f;
     private float _lastIntensity = 1.0f;
 
     private float _timePassed = 0.0f;
 
     private Light _lt;
     private const double Tolerance = 0.0001;
 
     private void Start() {
         _lt = GetComponent<Light>();
         _lastIntensity = _lt.intensity;
         FixedUpdate();
     }
 
     private void FixedUpdate() {
         _timePassed += Time.deltaTime;
         _lt.intensity = Mathf.Lerp(_lastIntensity, _targetIntensity, _timePassed/AccelerateTime);
 
         if (Math.Abs(_lt.intensity - _targetIntensity) < Tolerance) {
             _lastIntensity = _lt.intensity;
             _targetIntensity = Random.Range(MinLightIntensity, MaxLightIntensity);
             _timePassed = 0.0f;
         }
     }
 }
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

1 Person is following this question.

avatar image

Related Questions

GUI Problem 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Need help with Flashlight Script 1 Answer

Light.intensity and Lumen comparison 0 Answers

Incorrect Intensity value of Light component loaded from Assetbundles 0 Answers


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