Mathf.Lerp doesn't work on light object's value change

I am trying to make a smooth transition between changing a light’s range value when a lid is on or off a flame. It doesn’t really work right now cause it jumps straight to 8 from 2. Well it’s really something like 2.121 → 7.98 but you get the point. What I’m doing wrong?

public Animator anim;
public bool lidCheck;
public Light myLight;
public float minimum = 2.0f;
public float maximum = 8.0f;
public float smooth = 2.0f;

void Start()
{
    anim = GetComponent<Animator>();
    lidCheck = false;
}

void Update()
{
    if (lidCheck == false)
    {
        myLight.range = Mathf.Lerp(minimum, maximum, Time.deltaTime * smooth);
    }
    if (lidCheck == true)
    {
        myLight.range = Mathf.Lerp(maximum, minimum, Time.deltaTime * smooth);
    }
}

void OnTriggerStay (Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        if (Input.GetKey(KeyCode.E))
        {
            anim.Play("openlid");
            lidCheck = true;
        }

        if (Input.GetKey(KeyCode.R))
        {
            anim.Play("closelid");
            lidCheck = false;
        }
    }
}

}

Try

myLight.range = Mathf.Lerp(myLight.range , maximum, Time.deltaTime * smooth);

Lerp as a function goes from Point A to Point B by a value of 0 to 1.

In you line, inside the Update loop

myLight.range = Mathf.Lerp(minimum, maximum, Time.deltaTime * smooth);

suppose
minimum = 0.2;
maximum = 0.8;

and Time.deltaTime * smooth = 0.5; //i know its much less

Every single update your range will become

range = minimum + (maximum - minimum) * 0.5f;

And the next update you will start all over again, never reaching the target

Usually in lerps you want to feed the Point A the actual value that is chaning

I rewrote your code a bit. First, I changed your Update loop entirely. Next, I moved your code from OnTriggerStay to OnTriggerEnter with coroutine. The reason is that you should check for inputs (GetKey) in Update loop, not in FixedUpdate. OnTriggerStay acts as FixedUpdate, while OnTriggerEnter with coroutine (with while loop yielding null) works as Update. Here’s the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class tuomvii : MonoBehaviour
{
	public Animator anim;
	public bool lidCheck;
	public Light myLight;
	public float minimum = 2.0f;
	public float maximum = 8.0f;
	public float smooth = 2.0f;
	
	private float t;
	private Coroutine co = null;
	
	void Start()
	{
		anim = GetComponent<Animator>();
		lidCheck = false;
	}	
	
	void Update()
	{
		t += Time.deltaTime * smooth * (-1 + 2*System.Convert.ToInt32(lidCheck));
		t = Mathf.Clamp01(t);
		myLight.range = Mathf.Lerp(minimum, maximum, t);
	}
	
	void OnTriggerEnter (Collider other)
	{
		if (other.gameObject.CompareTag("Player"))
		{
			co = StartCoroutine(LightCoroutine());
		}
	}
	
	void OnTriggerExit(Collider other)
	{
		if (other.gameObject.CompareTag("Player"))
		{
			StopCoroutine(co);
		}
	}
		
	IEnumerator LightCoroutine()
	{
		while (true)
		{
			if (Input.GetKey(KeyCode.E))
			{
				anim.Play("openlid");
				lidCheck = true;
			}
			if (Input.GetKey(KeyCode.R))
			{
				anim.Play("closelid");
				lidCheck = false;
			}
			yield return null;
		}
	}
}

Hello there,

If you don’t want to use arbitrary speeds and values, you could implement something like this:

    private Coroutine cor_turnOnOrOff = null;
    public float f_transitionDuration = 4.0f;
    public Light ref_myLight = null;

    private void OnTriggerStay(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            if (Input.GetKey(KeyCode.E))
            {
                TurOnOrOff(true);
            }
            if (Input.GetKey(KeyCode.R))
            {
                TurOnOrOff(false);
            }
        }
    }

    public void TurOnOrOff(bool turnOn)
    {
        if (cor_turnOnOrOff != null)
            StopCoroutine(cor_turnOnOrOff);

        cor_turnOnOrOff = StartCoroutine(TurnOnOrOffCoroutine(turnOn));
    }

    private IEnumerator TurnOnOrOffCoroutine(bool turnOn)
    {
        float startValue = ref_myLight.range;
        float targetValue = 0.0f;
        float timer = 0.0f;

        if (turnOn)
        {
            anim.Play("openlid");
            targetValue = maximum;
        }
        else
        {
            anim.Play("closelid");
            targetValue = minimum;
        }

        while (timer < f_transitionDuration)
        {
            timer += Time.deltaTime;
            float t = timer / f_transitionDuration;
            t = t * t * t * (t * (6f * t - 15f) + 10f);
            ref_myLight.range = Mathf.Lerp(startValue, targetValue, t);
            yield return null;
        }
        yield return null;
    }

This might look a little complicated, but it isn’t really. All you have to do is call TurnOnOrOff(true); when you want the lamp to turn on, and TurnOnOrOff(false); when you want it off. The transition then occurs over however many seconds you set f_transitionDuration, instead of setting an arbitrary speed.

The line t = t * t * t * (t * (6f * t - 15f) + 10f); is just a smoothing algorithm. If you don’t like this one, you can pick another one from this page; keep the one that looks the best for your project!


I hope that helps!

Cheers,

~LegendBacon

Guys, I just found a solution for this. when you assign the result of this (Mathf.Lerp(a,b,t)) you shouldn’t do it to a new variable. for example If I want to set “k” to “M” by "t’ in every frame using Mathf.Lerp. This is what I’d do -
float K = 10;
float M = 30;

float t = 0.2f;

Update()
{
      Lerp();
}

void Lerp()
{
      K = Mathf.Lerp(K ,M, t * Time.deltaTime)`
      float m = K;
      Debug.Log(m);
}`

What happens is, I set the value returned by Mathf.Lerp to K which is the value I want to interpolate
to ITSELF. By this way, in every frame K is updated. If you assign this returned value to a new variable, this K will stay same forever and will give the same value every time you run the code as it is not changing withing the Mathf.Lerp method. this method does not change it’s arguments..
But, by assigning the returned value of the method to itself K is UPDATED EVERY FRAME. then you won’t stuck in the same value every time you run your code. If you want this K value for another variable, assign it just after the method. (I’ve done it as a comment) Debug.Log is the way how I took output here.