- Home /
Fading in between two materials overtime?
I am not able to make this code to work. Everything works except the transmission/fade, it does not appear. The material[0] just changes to material[1] without transmission. Is something wrong with my code, or does anyone has alternative way to make this work?
Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMaterial : MonoBehaviour
{
public Material[] material;
Renderer rend;
float duration = 0.5f;
// Start is called before the first frame update
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
rend.sharedMaterial = material[0];
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "zone1")
{
var lerp = Mathf.PingPong (Time.time, duration) / duration;
GetComponent<Renderer>().material.Lerp (material[0], material[1], lerp);
}
}
}
Answer by cublai · Mar 05, 2020 at 10:21 AM
It was because i was using Lightweight RP so i had to use a different syntax.
https://answers.unity.com/questions/1626209/using-colorlerp-with-lightweight-render-pipeline.html
Thanks a lot though to @Magso
Answer by Magso · Mar 03, 2020 at 11:08 PM
OnTriggerEnter()
is only called once, use OnTriggerStay()
which is called every frame when the trigger is intersected, likewise in the docs example Update()
is used.
Thank you. But it does not really make the effect i wanted. It looks like it still only skips to the other material. I thought that the duration will adjust the duration of the process of fading.
You have duration set to only half a second.
(Time.time, duration) / duration
works by dividing the duration by itself to make sure the returned output is always within 0 and 1 for e.g.
$$anonymous$$athf.PingPong (Time.time, 5)
will reach 5 in 5 seconds
$$anonymous$$athf.PingPong (Time.time, 5) / 5
will reach 1 in 5 seconds
Not sure why, but it still does not work, the material just changes to the second material without transmission. :/
Answer by cublai · Mar 04, 2020 at 09:30 AM
Thank you for your time!
Here is my current code, the OnTriggerExit does not work. I did try to set duration to 3f, but now nothing happens. Only when it is set to 0f it changes directly to material[1]. But can not make it to work as its fading to another material. Kinda loosing hope about this. Do you know about any other way to make this work?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMaterial : MonoBehaviour
{
public Material[] material;
Renderer rend;
float duration = 3f;
// Start is called before the first frame update
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
rend.sharedMaterial = material[0];
}
void OnTriggerStay(Collider col)
{
if (col.gameObject.tag == "zone3")
{
var lerp = Mathf.PingPong (Time.time, duration) / duration;
GetComponent<Renderer>().material.Lerp (material[0], material[1], lerp);
}
}
void OnTriggerExit(Collider col)
{
if (col.gameObject.tag == "zone3")
{
var lerp = Mathf.PingPong (Time.time, duration) / duration;
GetComponent<Renderer>().material.Lerp (material[1], material[0], lerp);
}
}
}
OnTriggerExit does not work
Because like OnTriggerEnter()
it only calls once on a single frame.
I've tested this code and it works.
public $$anonymous$$aterial[] material;
public float duration;
void OnTriggerStay()
{
var lerp = $$anonymous$$athf.PingPong (Time.time, duration) / duration;
GetComponent<Renderer>().material.Lerp (material[0], material[1], lerp);
}
is the if(col.gameObject.tag == "zone3")
correct?
@$$anonymous$$agso $$anonymous$$ight it be, cause i am using Lightweight RP?
Your answer
![](https://koobas.hobune.stream/wayback/20220612224336im_/https://answers.unity.com/themes/thub/images/avi.jpg)