- Home /
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?
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).
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.
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.
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
'time = 0.00' returned Color = [255,0,0]
'time = 0.25' returned Color = [191,0,0]
'time = 0.50' returned Color = [128,0,0]
'time = 0.75' returned Color = [64,0,0]
'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.
'time = 0.00' returned Color = [255,0,0]
'time = 0.25' returned Color = [191,0,0]
'time = 0.50' returned Color = [96,0,0]
'time = 0.75' returned Color = [24,0,0]
'time = 1.00' returned Color = [0,0,0]
Actual color values might be rounded differently, this is only to illustrate the point :p
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
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.
Your answer