Decreasing a value over time, called by InvokeRepeating
I'm attempting to create an effect in my game in which two discreet spot lights lower and raise their intensity every so often to give the impression that the player is actually travelling through space.
The method I'm using does work to an extent;
void Start ()
{
InvokeRepeating("LightChange", 5, 30);
}
void LightChange()
{
if (Random.value >= 0.5f)
{
dirLight1.intensity -= (Mathf.Clamp(0.05f * Time.deltaTime, 0.00f, 0.5f));
dirLight2.intensity += (Mathf.Clamp(0.05f * Time.deltaTime, 0.00f, 0.5f));
}
else
{
dirLight1.intensity += (Mathf.Clamp(0.05f * Time.deltaTime, 0.00f, 0.5f));
dirLight2.intensity -= (Mathf.Clamp(0.05f * Time.deltaTime, 0.00f, 0.5f));
}
}
It works in that the function is called and the value for the light intensity does indeed change, although it only changes for a moment (from .5 to .45) whereas the intended effect is that the values would decrease/increase to their minimum/maximum respectively.
Do you want a sudden change in intensity like from 0.4 to 0.1 or should it slowly change over time? The code you have here is going to bounce a little bit since Time.deltaTime is the time that has passed since the last frame so it is only going to give a change if the framerate is changing a lot. Perhaps $$anonymous$$athf.PingPong
is useful for you.