0
\$\begingroup\$
private bool alreadyFading = false;

Then

private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Teleporting Object")
        {

            if (!alreadyFading) StartCoroutine(Teleport(-3, 3, 5f));
        }
    }

And

IEnumerator Teleport(float from, float to, float duration)
    {
        alreadyFading = true;

        var timePassed = 0f;
        while (timePassed < duration)
        {
            var factor = timePassed / duration;
            var value = Mathf.Lerp(from, to, factor);

            material.SetFloat("_DissolveAmount", value);

            yield return null;
        }

        material.SetFloat("_DissolveAmount", to);

        alreadyFading = false;
    }

Using a break point it's getting to the Teleport function but it's not changing the values from -3 to 3. It does nothing to the shader to the property _DissolveAmount.

I want it to change the value from -3 to 3 with duration once not ping pong. but it's not just it's just don't do anything.

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

I think the main issue here is that you're not incrementing the variable timePassed so the Coroutine is stuck infinitely in the while loop.

As for the yield return nullI think it's best explained here but basically once a coroutine has yielded, the Main Thread continues running. On the next frame, Unity will find stacked coroutine and will call them from where they left off at the yield.

I suggest you change it to this (didn't test the code but it should hopefully work)

IEnumerator Teleport(float from, float to, float duration)
    {
        alreadyFading = true;

        var timePassed = 0f;
        while (timePassed < duration)
        {
            var factor = timePassed / duration;
            var value = Mathf.Lerp(from, to, factor);

            material.SetFloat("_DissolveAmount", value);
            timePassed += Time.deltaTime;
            yield return null;
        }

        material.SetFloat("_DissolveAmount", to);

        alreadyFading = false;
    }

Time.deltaTime is the amount of seconds it took for the engine to process the previous frame

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .