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 Lupus · Jun 29, 2014 at 04:02 AM · colorcolor changecolor.lerp

Color.Lerp doesn't go until white. Why?

I have this code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GrowPlayer : MonoBehaviour {
     
     private const string DESERT_STRING = "Desert";
     private const string SNOW_STRING = "Snow";
     private const float COLOR_STEP = (float)0.01;
     private const float LERP_STEP = (float)0.01;
     public Color myRed = new Color((float)0.9411765, (float)0.1960784, (float)0.1960784);
     public Color myYellow = new Color((float)0.8529412, (float)0.8529411, (float)0.3073097);
     public Color myGreen = new Color((float)0.2117647, (float)0.4588236, (float)0.244138);
     public Color myWhite = new Color((float)1,(float)1,(float)1);
     private List<GameObject> areasList = new List<GameObject>();
     public Color thisColor;
     
     // Use this for initialization
     void Start () {
         foreach (Transform child in transform) {
             child.gameObject.AddComponent<GrowPlayer>();
         }
 
         GameObject[] objects = GameObject.FindGameObjectsWithTag(DESERT_STRING);
         foreach (GameObject go in objects) {
             areasList.Add(go);
         }
         
         objects = GameObject.FindGameObjectsWithTag(SNOW_STRING);
         foreach (GameObject go in objects) {
             areasList.Add(go);
         }
     }
     
     // Update is called once per frame
     void Update () {
         thisColor = renderer.material.color;
         colorChange();
     }
 
     void colorChange () {
         bool findedOne = false;
         foreach(GameObject area in areasList) {
             if (area.collider.bounds.Contains(renderer.transform.position)) {
                 findedOne = true;
                 switch (area.tag) {
                 case DESERT_STRING:
                     goToColor(myYellow);
                     break;
                 case SNOW_STRING:
                     goToColor(Color.white);
                     break;
                 default:
                     goToColor(myGreen);
                     break;
                 }
             }
             if (!findedOne) {
                 goToColor(myGreen);
             }
         }
     }
 
     void goToColor(Color color) {
         renderer.material.color = Color.Lerp(renderer.material.color, color, LERP_STEP);
     }
 
 }


It changes the renderer color depending on where it is. It works how it should for "myYellow" and "myGreen", but it never reaches the white color, it stops much before becoming white. Does anyone knows what could be the problem?

Comment
Add comment · Show 1
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 robertbu · Jun 29, 2014 at 04:31 AM 0
Share

Not sure if this is the issue, but the way you are using Color.Lerp() produces an eased movement towards the color. It can take a really long time to reach the end value. Also you are not using a Time.delaTime adjusted value, so your fade to the color may be uneven.

2 Replies

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

Answer by Maerig · Jun 30, 2014 at 06:25 AM

This is a tricky logic issue. Let's execute the algorithm manually supposing the following :

  • The gameObject is only contained in an area with the "Snow" tag

  • The areasList member contains those gameObjects, in this order :

    1. GameObject with the "Desert" tag

    GameObject with the "Snow" tag

    bool findedOne = false;

First, findedOne is set to false.

 foreach(GameObject area in areasList) {

Then, the algorithm iterates through the list. The first item is the desert area, so

 (area.collider.bounds.Contains(renderer.transform.position))

is false. However,

 !findedOne

is true, so

 goToColor(myGreen);

will be executed. Then, in the next foreach iteration

 area.collider.bounds.Contains(renderer.transform.position)

is true, so eventually

 goToColor(Color.white);

will be executed too.

To fix it, you just need to take

 if (!findedOne) {
     goToColor(myGreen);
 }

out of the foreach loop, like this

 void colorChange () {
     bool findedOne = false;
     foreach(GameObject area in areasList) {
         if (area.collider.bounds.Contains(renderer.transform.position)) {
             findedOne = true;
             switch (area.tag) {
             case DESERT_STRING:
                 goToColor(myYellow);
                 break;
             case SNOW_STRING:
                 goToColor(Color.white);
                 break;
             default:
                 goToColor(myGreen);
                 break;
             }
             /* (Optional) Exit the foreach loop, so that it doesn't
                keep on looking for an intersected area once one has
                been found */
             break;
         }
     }
     // Only fade to green if the gameObject isn't contained in any area
     if (!findedOne) {
         goToColor(myGreen);
     }
 }

This way, the gameObject will only fade to green if it isn't contained in any area.
Does that help ?

Comment
Add comment · Show 1 · 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 Lupus · Jun 30, 2014 at 07:40 PM 0
Share

I wouldn't call it a logic issue. I would call an attention issue. The green change should be out of the loop from the beginning. I didn't notice I have put it inside the loop by mistake. Thank you.

avatar image
0

Answer by hav_ngs_ru · Jun 29, 2014 at 08:09 AM

Lerp usage: Lerp(srcColor, targetColor, dt);

if dt == 0 - you will got a srcColor as result

if dt == 1 - you will have a targetColor as result

if 0 < dt < 1 - you will have a color between src and target color (smaller dt - color much like a src, greater dt - color much like a targetColor)

in your case you will endlessly approach targetColor but never reach it.

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 robertbu · Jun 29, 2014 at 08:35 AM 0
Share

@hav_ngs_ru - the OP source is a non-traditional use of Lerp(), but it is used and many examples on this site. It works by updating the start position each frame, and moving some fraction of the distance towards the goal. In this case the fraction is 0.01. So each frame the same fraction is moved, but that fraction represents ever decreasing values as the Color moves towards the destination color.

avatar image hav_ngs_ru · Jun 30, 2014 at 05:36 AM 0
Share

robertbu, yes, such use of Lerp is clear for me, but question was - why color does not reach the destination. the answer is - because in this case the color change slows down when approaching the destination, and the speed drops to zero just before reaching the "white color". so if the goal is to reach white color - there is at least two ways: to change color linearly, or increase the fraction if its important to change color this way (such in code).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Color.lerp not working properly 2 Answers

How do you Lerp light.color into multiple colors? 2 Answers

Change Color of Children Based on Parent 0 Answers

Change slider color to green when value is 50 2 Answers

Not exact color 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