- Home /
Question by
Alpha-16 · Aug 02, 2021 at 03:05 PM ·
coroutinematerialslerpmaterial renderer
Lerping multiple materials of a gameobject within a coroutine
Hello all,
I build a simple game where the player can jump on platforms. Once the player touch a platform I want to lerp the two materials with a new red warning material. I'm doing this into a Coroutine to not stop the whole game execution. Atm, I'm having issues on the lerp part. It's simply not working.
public IEnumerator WarningMaterial(Renderer rend, Material mat01, Material mat02)
{
Debug.Log("Coroutine Starts");
Debug.Log(mat01);
Debug.Log(mat02);
Material matWarning = Resources.Load("Materials/M_Warning", typeof(Material)) as Material;
Debug.Log(matWarning);
float lerp = Mathf.PingPong(Time.time, .5f) / .5f;
rend.material.Lerp(mat01, matWarning, lerp);
rend.material.Lerp(mat02, matWarning, lerp);
yield return new WaitForSeconds(gameController.blockLifeDuration);
Debug.Log("Coroutine ends");
DestroyBlock();
}
void FixedUpdate()
{
if (playerOnPlatform || outOfPlayerRange && !GameController.gameOver)
{
if (!this.isBlockSpawn || (this.id != 0 && !this.isBlockSpawn)) // Avoid warning material on first block
{
Material mat01 = GetComponent<Renderer>().materials[0];
Material mat02 = GetComponent<Renderer>().materials[1];
Renderer rend = GetComponent<Renderer>();
StartCoroutine(WarningMaterial(rend, mat01, mat02));
}
}
All the debugs are working as expected and do not return Null.
I tried different methods on the net but still not working.
Thanks!
Comment