Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Headrush95 · Jul 22, 2020 at 03:26 PM · unity 5lerpcolor changecolor.lerp

smoothly switch between 2 colors that are using lerp

i have a simple script that changes the emission color based on a bool, the first color is its idle color which is a lerp that goes from orange to light purple.

the second color is its "isinDanger" state and this lerps between orange and red, mkis there any way to make the transition between both of these color variants smooth. when the bool is set to true the colors switch instantly with no transition and iv'e tried numerous things to try to get them to be smooth but ultimately it just breaks the lerping.

 using System.Collections; using System.Collections.Generic; using UnityEngine;
 
 public class GemGlowOrange : MonoBehaviour { public Material Mat;
 
 Color Red;
 
 Color lightpurple;
 
 Color orange;
 
 public bool isInDanger = false;
 
 Color CurrentColor; float startTime;
 
 public float speed = 1.0f;
 
 void Start() {
 
 startTime = Time.time; 

 orange = new Color(1.320755f, 0.5346785f,0.2928088f,1f); 

 Red = new Color(0.858f, 0f,0f,1f);
 
 lightpurple = new Color(1.320755f,0.2973427f,0.8263685f,1f);
 
 var Mat = GetComponent<Renderer>().sharedMaterial; 
 
 
 
 Mat.EnableKeyword("_EMISSION"); 
 }
 
 void Update() {
 
 float frac = (Mathf.Sin(Time.time - startTime) * speed);
 
 CurrentColor = Mat.GetColor("_EmissiveColor");
 
 if(!isInDanger){
 Mat.SetColor("_EmissiveColor", Color.Lerp(lightpurple, orange, frac));
 
 } else if (isInDanger) {
     
 
 
    
 Mat.SetColor("_EmissiveColor", Color.Lerp(Red, orange, frac));
 
 }
 
 } }
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

2 Replies

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

Answer by Headrush95 · Jul 29, 2020 at 11:23 PM

hi zeal i managed to fix this with some help from someone on another forum the code i ended up using was this and it works wonderfully!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GemGlowOrange : MonoBehaviour
 {
 public Material Mat;
 
 Color Red;
 
 
 
 Color lightpurple;
 
 Color orange;
 
 public bool isInDanger = false;
 
 Color CurrentColor;
 float startTime;
 
 public float speed = 1.0f;
 
 public float DangerLevel;
 
 
 
 void Start() {
 
     startTime = Time.time; // time since game started
 
 orange =  new Color(1.320755f, 0.5346785f,0.2928088f,1f); // making my colours RGBA
 
 Red = new Color(0.858f, 0f,0f,1f);
 
 lightpurple = new Color(1.320755f,0.2973427f,0.8263685f,1f);
 
 
     var Mat = GetComponent<Renderer>().sharedMaterial; // making a reference to the material so we can enable the emission keyword if you do not want this to affect every object using this
     //material then use "Getcomponent<Renderer>().material" the same goes with setting the color as you can see further down.
 
  
 
     Mat.EnableKeyword("_EMISSION"); // this is making sure this shader has the emission keyword enabled.
   
 
 
 
 
 
 }
 
 void Update() {
 
 
 
 float TransitionDuration = 0.5f;
 
 
 DangerLevel = Mathf.MoveTowards(
     DangerLevel,
     isInDanger ? 0f : 1f,
     Time.deltaTime / TransitionDuration);
 
     float frac = (Mathf.Sin(Time.time - startTime) * speed);
 
 
 Color DangerColor = Color.Lerp(lightpurple,Red, DangerLevel);
 
 
 
     
 
 Mat.SetColor("_EmissiveColor", Color.Lerp(DangerColor, orange, frac)); 
 
 
          
 
 }
 
  
  
 
 
  
 
 
 
     
 }
 
 
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
0

Answer by WaqasHaiderDev · Jul 23, 2020 at 01:06 AM

Hi, Did you try coroutines? I have not tried your code but usually for execution of functions where you want to spread execution over multiple frames instead of completing the function in one frame for what so ever reason, coroutines are used. Below is a document from untiy, which even discussed a case similar to yours. Just read out first few paragraphs and tell if this solved your issue. Coroutines Unity

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

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

228 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 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

Control time transition between colors 1 Answer

Color.lerp not working properly 2 Answers

Colour Lerp on 2D point lighting for fire effect. 1 Answer

Color.Lerp doesn't work 1 Answer

If statement not being fullfilled until GameObject inspected in inspector. 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