- Home /
Changing light intensity over time via mathf.lerp
Hi guys, sorry to bother. I really hope this is not too stupid, but I don't manage to get it right. So, what I want to do: if [Event] change the intensity of a point light from 0.01 to 1.5 over x seconds under a certain condition.
My Code:
public class LightWall : MonoBehaviour {
// public float timer = 10.0f;
public Light lt;
// Use this for initialization
void Start ()
{
lt = GetComponent<Light>();
}
// Update is called once per frame
void Update ()
{
// FIRST METHOD THAT DID NOT WORK
// timer -= Time.deltaTime;
// if (timer <= 0)
// LightIntensity ();
// ANOTHER TRY VIA PUBLIC STATIC BOOL
// if (outer_fadein.fadeBegin == true)
LightIntensity ();
}
void LightIntensity ()
{
lt.intensity = Mathf.Lerp (0.01f , 1.5f , Time.time/5);
Debug.Log ("LightIntensity =" + lt.intensity + Time.time);
}
}
The Problem: If I simply call the function in Update, it will increase the light perfectly fine from 0.01 to 1.5. But I only want this to happen after 10 seconds, or better, after fadebegin is set to true. When I try it with one of the two conditions, the light intensity apruptly strikes in instead of beginning at 0.01. Don't know if it's valuable info, but: outer_fadein.fadeBegin will be set to true after 10.00 seconds.
Thanks for the help, I really appreciate!
Cheers!
EDIT: I think the problem is Time.time in
lt.intensity = Mathf.Lerp (0.01f , 1.5f , Time.time/5);
Since Time.time (which is in my understanding global time since app started) is not 0 when fadeBegin is set to true, intensity will not begin at 0.01f, correct?
How do I solve this? I definetly want this fade to be controlled over a time based value and not being dependant on framerates.
light.intensity = $$anonymous$$athf.Lerp(light.intensity, targetIntensity, fadeSpeed * Time.deltaTime);
just a copy from unity staff, but this might help.
Answer by hnm938 · Jul 26, 2019 at 09:08 PM
I think I might be a little too late to help but for anyone that still is looking at this for help like I was, you were right about Time.time being wrong this would be how you fix it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightFlicker : MonoBehaviour
{
public float maxRange = 11;
public float minRange = 6;
public float flickerSpeed = 0.5f;
private Light lightSource;
public void Start()
{
lightSource = GetComponent<Light>();
}
public void Update()
{
lightSource.intensity = Mathf.Lerp(minRange, maxRange, Mathf.PingPong(Time.time, flickerSpeed));
}
}
I used this for my campfire flicker script, for something like fire setting the maxRange to 11 and the minRange to 6 with a speed of 0.5 is pretty good.
Your answer
Follow this Question
Related Questions
Stop and Resume Timer 0 Answers
How to reset Time.time ? 1 Answer
Help with time.time when new scene starts. 1 Answer
Why Won't This Message Log Into My Console? 1 Answer
Time based scoring 2 Answers