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

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:

        public float minIntensity;
        public float maxIntensity;

        public float intensityAmount;

        private Light light;

        void Awake(){
            light  = GetComponent<Light>();
        }

        void Update(){

             if (light.intensity > maxIntensity) {
                 light.intensity -= intensityAmount * Time.deltaTime;
             }
             else if (light.intensity < minIntensity) {
                light.intensity += intensityAmount * Time.deltaTime;
             }

          }

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.

1 Like

@LeftyRighty Sorry, But this function always starts from 0 :frowning:

Maybe use an animationCurve

light.intensity = curve.Evaluate(Time.time);

1 Like

Use the PingPong value as your control variable in Mathf.Lerp

PingPong method always starts from 0. I need values between 4 and 7.

public float minIntensity = 4;
public float maxIntensity = 7;

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

I can not understand what you meant, you could give me an example, Please?

float value = Mathf.PingPong(Time.time, 1) * 3 +4;
1 Like

Like a boss @JamesLeeNZ :

light.intensity = Mathf.PingPong(Time.time * 2, 1) * 3 + 4;

Works perfectly! Thanks so much! :slight_smile:

glad to be of help

1 Like

What I was getting at, by the way, was this:

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.

1 Like

Nice too, @StarManta :wink: UP

1 Like