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 MichaelMcShane · Aug 11, 2013 at 06:06 AM · colortimeinterpolationunderstanding logic

Interpolating from one color to the other repeatedly

What I want to do: I am trying to interpolate the color of a cube from green to red and back. Between each of these interpolations is a 4 second pause.

What I am seeing: When the game starts the cube remains green for 4 seconds then suddenly shifts to red. Afterwards it interpolates from red to green and back but without the 4 second pause.

My Code

 private const float TRANSITION_PERIOD = 1;
 private const float GREEN_TO_RED = 2;
 private const float RED_TO_GREEN = 3;
 private float playerAnimState = 0;
 private float colorDuration = 4;    
 private float mLerp = 0;
 private float time = 0;
 private Color playerColor;

Start function:

 renderer.material.color = Color.green;
 playerAnimState = TRANSITION_PERIOD;

Update function:

     playerColor = this.renderer.material.color;
         
     if ( playerAnimState == TRANSITION_PERIOD )
     {
         time += Time.deltaTime;
         if ( time >= colorDuration )
         {
             time = 0;
             if ( playerColor == Color.green )
             {
                 playerAnimState = GREEN_TO_RED;
             }
             else
             {
                 playerAnimState = RED_TO_GREEN;    
             }
         }
     }
     else if ( playerAnimState == GREEN_TO_RED )
     {
         mLerp = Mathf.PingPong( Time.time, interpolateDuration ) / interpolateDuration;
         renderer.material.color = Color.Lerp ( Color.green, Color.red, mLerp );
         playerColor = renderer.material.color;
         if ( playerColor == Color.red )
         {
             playerAnimState = TRANSITION_PERIOD;    
         }
     }
     else if ( playerAnimState == RED_TO_GREEN )
     {
         mLerp = Mathf.PingPong( Time.time, interpolateDuration ) / interpolateDuration;
         renderer.material.color = Color.Lerp ( Color.red, Color.green, mLerp );    
         playerColor = renderer.material.color;
             
         if ( playerColor == Color.green )
         {
             playerAnimState = TRANSITION_PERIOD;    
         }
     }

Thanks for the help. I have gone through my logic on paper and it seems correct. I suspect it may have something to do with Time but am not sure what could be causing it to misbehave. I have tried separating this code from the overall project but this problem still occurs.

Comment
Add comment · Show 2
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 Eric5h5 · Aug 11, 2013 at 06:14 AM 0
Share

Probably a coroutine such as Fade would be far easier.

avatar image MichaelMcShane · Aug 12, 2013 at 12:28 AM 0
Share

Thanks for the link. I'm going to spend a little more time seeing if I can figure this out. If not I'll go right to Fades. Thanks.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by blastproofgames · Nov 02, 2017 at 02:25 PM

I might be late to the party, but why not both Lerp and PingPong?

         var pingPong = Mathf.PingPong(Time.time, 1);
         var color = Color.Lerp(BaseColor, InterpolatingColor, pingPong);
         image.color = color;
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 ZeroSumGames · Aug 11, 2013 at 07:09 AM

I think your solution may be overly complicated. To Lerp you want to move the third Color.Lerp parameter from 0 to 1 over the time period. I don't think you need to use PingPong for this. I bet if you attached a debugger you'd be seeing mLerp getting set to some funky value that messes your code up.

Try something like this.

 float duration = interpolateDuration;
 if ( playerAnimState == GREEN_TO_RED )
 {
 
        duration -= Time.deltaTime;
        mLerp = (interpolateDuration - duration ) / interpolateDuration;
        
 
        renderer.material.color = Color.Lerp ( Color.green, Color.red, mLerp );
        playerColor = renderer.material.color;
        if ( mLerp > = 1)
        {
          playerAnimState = TRANSITION_PERIOD; 
          duration = 1.0f;
        }
     }
 
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 MichaelMcShane · Aug 12, 2013 at 12:27 AM 1
Share

I tried this method with and without PingPong and printed mLerp both times. When I did use PingPong the square changed from green to red after 4 seconds. However it never changed back to green. mLerp was: 0 (until it changed to red), .992xxxx, .996xxxx, .993xxxx, .995xxxx

It repeated this pattern where the first two digits stayed .99.

When I tried it without PingPong mLerp was: 0 (for 4 seconds), .0522xxxx

The digits beyond .0522 would increase/decrease.

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

17 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

make an event occur after so many seconds? 1 Answer

Changing two different objects renderer colour 1 Answer

Renderer and Color are unknown to unity. Why? 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