Search Unity

Unity3D - How to control Light.Intensity without animation (script)

Discussion in 'Scripting' started by IgorUnity3D, Aug 31, 2015.

  1. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    I have the following animation:



    Note: Below image of animation control


    How can I get the same result using a script? My intention is that the script has no "if".

    Don't like this:

    Code (CSharp):
    1.  
    2.         public float minIntensity;
    3.         public float maxIntensity;
    4.  
    5.         public float intensityAmount;
    6.  
    7.         private Light light;
    8.  
    9.         void Awake(){
    10.             light  = GetComponent<Light>();
    11.         }
    12.  
    13.         void Update(){
    14.  
    15.              if (light.intensity > maxIntensity) {
    16.                  light.intensity -= intensityAmount * Time.deltaTime;
    17.              }
    18.              else if (light.intensity < minIntensity) {
    19.                 light.intensity += intensityAmount * Time.deltaTime;
    20.              }
    21.  
    22.           }
    23.  
    I wonder if there is any possibility to do this using some native function ... like: (Math.Clamp, Math.Lerp, Quaternion.Slerp) without any condition as "if" in the code.

    Thank you in advance.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
  4. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    Maybe use an animationCurve

    light.intensity = curve.Evaluate(Time.time);
     
    ionside likes this.
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Use the PingPong value as your control variable in Mathf.Lerp
     
  6. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    PingPong method always starts from 0. I need values between 4 and 7.

    Code (CSharp):
    1.  
    2. public float minIntensity = 4;
    3. public float maxIntensity = 7;
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    well, if pingpong goes between 0-1 you can multiply that value by 3 then add 4.

    ie, use the pingpong value as a percentage value
     
  8. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    I can not understand what you meant, you could give me an example, Please?
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Code (csharp):
    1.  
    2.  
    3. float value = Mathf.PingPong(Time.time, 1) * 3 +4;
    4.  
    5.  
     
    IgorUnity3D likes this.
  10. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    Like a boss @JamesLeeNZ :

    Code (CSharp):
    1. light.intensity = Mathf.PingPong(Time.time * 2, 1) * 3 + 4;
    Works perfectly! Thanks so much! :)
     
  11. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    glad to be of help
     
    IgorUnity3D likes this.
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    What I was getting at, by the way, was this:
    Code (csharp):
    1. light.intensity = Mathf.Lerp (minIntensity, maxIntensity, Mathf.PingPong(Time.time * 2, 1) );
    The advantage over JamesLeeNZ's is that it would allow you to easily change the min and max values in the inspector.
     
    IgorUnity3D likes this.
  13. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    Nice too, @StarManta ;) UP
     
    Thiago-Garcia likes this.