Hi,
I have a shader with an emission that I am trying to get to pulse stronger and weaker, and have thus a pow with a strength exposed in the shader material. The script is like this:
public class Pulse : MonoBehaviour {
private float pulse;
public float scale = 0.1f;
public bool run = false;
// Use this for initialization
void Start () {
pulse = renderer.material.GetFloat("_Strength");
}
void Update()
{
if(run)
{
if (pulse < 0.5f || pulse > 2.0f)
{
scale *= -1.0f;
}
// Set Scale, which is how much the pulse will increase and decrease
pulse += scale;
// Feed the float of pulse into the _Strength variable in the material/shader
renderer.material.SetFloat("_Strength", pulse);
if (pulse < 0.5f || pulse > 2.0f)
{
print(pulse); run = false;
}
}
else
StartCoroutine(check());
}
IEnumerator check()
{
print("Yield before, pulse: " + pulse + ", Scale: " + scale);
yield return new WaitForSeconds(2.0f);
print("Yield after, pulse: " + pulse + ", Scale: " + scale);
run = true;
}
The pulse is ment to go between a strength of 0.5 and 2, where it should stop at either extremes for a few seconds, before going to the other extreme. Our problem is that it currently stops at one extreme, 0.5, then goes up to 2 but goes down to 0.5 again without waiting, then waits at 0.5 again.. so it is only stopping at one extreme. Any idea as to why? What am I not seeing T_T
Thanks for catching the Time.deltaTime, totally forgot about that xD The yield works, but it sometimes got caught in an endless loop as the pulse was always either over 2.0f or below 0.5f and it couldn't get back to a number between 0.5f and 2.0f. Fixed it by resetting pulse to either 0.51f or 1.99f after the pause (depending on where it was), and it works like a charm now :D Thanks for the answer :)
– wolferey