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 PaxForce · Dec 11, 2014 at 10:00 AM · lerpcolor.lerp

Color LERP problem

I want to show/hide one of the enemies in my intro scene by LERPing a guiTexture (positioned in front of the enemy) from black to clear and clear to black. This is my script:

 using UnityEngine;
 using System.Collections;
 
 public class ScreenFadeInOut_scr : MonoBehaviour {
 
     private float fadeStart;
     private bool isFading = false;
     public float fadeDuration;
     private bool enemyVisible = false;
 
      
     void Awake()
     {
         guiTexture.pixelInset = new Rect(0, Screen.height - Screen.height / 2.3f, Screen.width / 4.4f, Screen.height / 2.3f);
         fadeDuration = 10f;
     }
 
     void Start()
     {
         
     }
     void Update()
     {
         if (!enemyVisible && isFading)
         {
             ShowEnemy();
             
         }
         else if (enemyVisible && isFading)
         {
             HideEnemy();
             
         }
             
     }
 
     void ShowEnemy()
     {
         float timeSinceStarted = Time.time - fadeStart;
         float percentageComplete = timeSinceStarted / fadeDuration;
 
         guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, percentageComplete);
         Debug.Log("percentageComplete = " + percentageComplete);
 
         if(percentageComplete >= 1f)
         {
             isFading = false;
             enemyVisible = true;
             Debug.Log("enemyVisible = " + enemyVisible);
             guiTexture.enabled = false;
         }
     }
 
     void HideEnemy()
     {
         guiTexture.enabled = true;
         
         float timeSinceStarted = Time.time - fadeStart;
         float percentageComplete = timeSinceStarted / fadeDuration;
 
         guiTexture.color = Color.Lerp(guiTexture.color, Color.black, percentageComplete);
         Debug.Log("percentageComplete = " + percentageComplete);
 
         if (percentageComplete >= 1f)
         {
             isFading = false;
             enemyVisible = false;
             Debug.Log("enemyVisible = " + enemyVisible);
         }
     }
 
 
     void OnGUI()
     {
         if (GUI.Button(new Rect(Screen.width / 4, Screen.height / 3, 50, 50), "S/H"))
         {
             fadeStart = Time.time;
             isFading = true;
         }
     }
 }


When I click the Show/Hide button the percentageComplete variable goes all the way (full 10 seconds), but the enemy becomes visible/invisible in just a few seconds (1.5 to 3). How do I fix this?

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

1 Reply

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

Answer by troien · Dec 11, 2014 at 10:59 AM

Because in this line

 guiTexture.color = Color.Lerp(guiTexture.color, Color.black, percentageComplete);

Instead of picking a color in between start and end color, you pick a color in between current and end color. Which in your case is not what you want to do. So changing it to this

 guiTexture.color = Color.Lerp(Color.clear, Color.black, percentageComplete);

should help. (Change this for both hide and show).

Comment
Add comment · Show 6 · 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 PaxForce · Dec 11, 2014 at 11:34 AM 0
Share

a good idea and a working one too. however, could you explain this?

the enemy starts appearing only when half of the time percentageComplete passed. the spot on the screen remains black until then.

when I push the button again, the the guiTexture starts to gain alpha and the enemy has disappeared completely when half of the time percentageComplete has passed.

avatar image troien · Dec 11, 2014 at 12:11 PM 0
Share

What did you ask me to explain? what I did, or are you describing a new problem.

If you are describing a new problem, could you please specify the range of percentageComplete (at what value does it start, and at what value does it end)

To explain why my method above works:

this is because Lerp's third argument (time) is a value between 0 and 1.

  • 0 $$anonymous$$eans we return color A.

  1. means we return something in between Color A and B

  • 1 $$anonymous$$eans we return color B.

So 'time' is in this case a value that takes 10 second to move from 0 to 1.

And that is also the probem, sinse an Update is (at 60 fps) called 60 times per second. So a total of 600 times this Lerp method is called.

Lets say we want to lerp from red to black. And the 'time' value is increased by 0.25 every frame (Normally this is much slower I know, but this is to illustrate my point)

If you Lerp from start to end color, this is what happens

  1. 'time = 0.00' returned Color = [255,0,0]

  2. 'time = 0.25' returned Color = [191,0,0]

  3. 'time = 0.50' returned Color = [128,0,0]

  4. 'time = 0.75' returned Color = [64,0,0]

  5. 'time = 1.00' returned Color = [0,0,0]

If you Lerp from current color, then between point 2 and 3 etc. Ins$$anonymous$$d of picking 50% from Color A to B, you pick 50% of the last result and B. And sinse the last result is closer to B then A is, the speed at which is Lerped is increaed drastically.

  1. 'time = 0.00' returned Color = [255,0,0]

  2. 'time = 0.25' returned Color = [191,0,0]

  3. 'time = 0.50' returned Color = [96,0,0]

  4. 'time = 0.75' returned Color = [24,0,0]

  5. 'time = 1.00' returned Color = [0,0,0]

Actual color values might be rounded differently, this is only to illustrate the point :p

avatar image PaxForce · Dec 11, 2014 at 04:13 PM 0
Share

hey man I know what you meant, you didn't have to explain it like this :) I appreciate it though. What I was asking in my comment was this:

my guiTexture is positioned IN FRONT OF THE TARGET game object

THIS is how it should be:

 'time = 0.00' alpha = 0 // texture is INVISIBLE - we see the target as clearly as if there was nothing in front of it
 'time = 0.25' alpha = 0.25
 'time = 0.50' alpha = 0.5 // texture is "half-visible"
 'time = 0.75' alpha = 0.75
 'time = 1.00' alpha = 1 // texture is VISIBLE - we can't see the target at all

  

but THIS is how it behaves using my code:

 'time = 0.00' alpha = 0 // texture is INVISIBLE - we see the target as clearly as if there was nothing in front of it
 'time = 0.25' alpha = 0.25 // texture is "half-visible"
 'time = 0.50' alpha = 0.5 // texture is VISIBLE - we can't see the target at all
 'time = 0.75' alpha = 0.75 // texture remains absolutely VISIBLE - we can't see the target at all
 'time = 1.00' alpha = 1 // texture remains absolutely VISIBLE - we can't see the target at all


avatar image troien · Dec 11, 2014 at 08:25 PM 0
Share

Oh that, yea... I forgot. For some reason, don't ask me why... (because I don't know really) This happens with GUITextures (The reason why I never used them). As you can also see when you add them, the default color has a opacity of 128, while it is completely opaque

If you want the answer to why that is, I would advise you to ask a new question? Or hope somewone who knows reads this :p

The workaround would be to either make percentageComplete range between 0 and 0.5 ins$$anonymous$$d of 0 and 1 (Sinse between 0.5 and 1 all you do is chaning pieces that should be semi transparent to opaque :p), or use something else like GUI.DrawTexture, or the 2D sprites even.

avatar image PaxForce · Dec 12, 2014 at 06:13 PM 0
Share

so it's a Unity issue, I presume...

Show more comments

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

26 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

Related Questions

smoothly switch between 2 colors that are using lerp 2 Answers

IndexOutOfRange when using Lerp 1 Answer

Color.Lerp everytime Onclick 1 Answer

If statement not being fullfilled until GameObject inspected in inspector. 0 Answers

Color.Lerp doesn't work 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