[Solved] Fading Two Materials Using Color
I am sharing this code because I had a hard time finding something like this. I hope it will help someone else.
Objective
Fade out first material when FOV changes
Change material to second material
Fade in second material to max alpha color
Note Second material started with an alpha of zero from Material Inspector.
Solution
Check when the FOV changes to start process. Start decreasing alpha color using linear method, once it reaches zero change to second material and fade in to max alpha.
Code
// Public variable paremeter for Unity inspector input
public float smooth;
Material worldMap;
public Material australiaMap;
//Private variables for script mod
private Renderer rend;
private float colorOrigin;
private Color colorWorldMap;
private Color colorAustraliaMap;
private float originFOV;
private float currentFOV;
// Use this for initialization
void Start()
{
//get tender component
rend = GetComponent<Renderer>();
//set variable with material at start
worldMap = rend.material;
//set variable with color at start
colorWorldMap = rend.material.color;
//set variable with initial FOV
originFOV = Camera.main.fieldOfView;
}
// Update is called once per frame
void Update()
{
//update alpha color every frame
colorOrigin = rend.material.color.a;
//update FOV every frame
currentFOV = Camera.main.fieldOfView;
//check if FOV has changed from start, if so start fading
if (currentFOV != originFOV)
{
//fade initial material until aplha color is zero and material is worlmap
if (colorOrigin > 0 & rend.material == worldMap)
{
//call function to fade out worlmap
StrayaFadeOut();
}
//when apha color is zero change material to Australia map
else
{
//Fade in Australia map function - Material starts with apha color at zero
StrayaFadeIn();
}
}
}
//function to fade out Worlmap
void StrayaFadeOut()
{
//set alpha color fade out using linear method to zero
colorWorldMap.a = Mathf.Lerp(colorOrigin, -smooth, Time.deltaTime);
//set material color to previous value
rend.material.color = colorWorldMap;
//change color origin to new alpha color
colorOrigin = colorWorldMap.a;
}
//function to fade ni australia map
void StrayaFadeIn()
{
//chage material to australia map
rend.material = australiaMap;
//set smooth to max alpha color value
smooth = 255;
//set variable with current alpha color
colorAustraliaMap = rend.material.color;
//fade in alpha color using linear method
colorAustraliaMap.a = Mathf.Lerp(0.0f, smooth, Time.deltaTime);
//change alpha color of current material
rend.material.color = colorAustraliaMap;
}
}
Feel free to comment and criticize the code. This was my solution, I was not satisfied with the others I googled. I do not know if this the most elegant way, but it worked like a charm.
Cheers
Your answer
![](https://koobas.hobune.stream/wayback/20220612110143im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Audio Fade With Toggle 1 Answer
I need help getting a camera to fade up from black after my player teleports to next room position. 2 Answers
Alpha Fade In distance problem 0 Answers
Screen Fader Problem 1 Answer
How to add a scene fader to a timer 0 Answers