Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by cublai · Mar 03, 2020 at 10:32 PM · materialtimefade

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);
     }
 }
 
 }

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
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
avatar image
1

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.

Comment
Add comment · Show 3 · Share
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
avatar image cublai · Mar 03, 2020 at 11:31 PM 0
Share

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.

avatar image Magso cublai · Mar 04, 2020 at 12:47 AM 0
Share

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

avatar image cublai cublai · Mar 04, 2020 at 07:43 AM 0
Share

Not sure why, but it still does not work, the material just changes to the second material without transmission. :/

avatar image
0

Answer by cublai · Mar 04, 2020 at 09:30 AM

@Magso

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);
      }
 
      }

}

Comment
Add comment · Show 2 · Share
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
avatar image Magso · Mar 04, 2020 at 01:27 PM 0
Share

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?

avatar image cublai · Mar 05, 2020 at 06:57 AM 0
Share

@$$anonymous$$agso $$anonymous$$ight it be, cause i am using Lightweight RP?

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

138 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 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 avatar image avatar image avatar image avatar image

Related Questions

Randomized weather, little stuck 1 Answer

material Instance only updates after changes, when the Material Component is expanded or folded 0 Answers

FadeIn color material not working? 1 Answer

Changing two different objects renderer colour 1 Answer

Gun Barrel To Get Hot - Change _Tint 1 Answer


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