Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by KevinKLV · Feb 02, 2017 at 09:57 AM · materialfadeoutfieldofviewmathf.lerpfadein

[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

  1. Fade out first material when FOV changes

  2. Change material to second material

  3. 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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

67 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges