- Home /
make a gameobject lerp transparency?
I'm trying to make my player gameobject flash/blink transparency (over 1 second, go from alpha 1.0 to 0.1 and back up to 1.0)
Firstly, I know to make an object transparent, you have to (and I have done):
1) change the object's material from diffuse to transparent -> diffuse
2) use the code:
Color tempcolor = gameobject.renderer.material.color
tempcolor.a = (float between 0.0 and 1.0); //0 is invis, 1 is fully visible
gameobject.renderer.material.color = tempcolor;
I then put this in a While loop (while my player's gameobject has not moved) and added in code to increase/decrease the float accordingly each time this was called. However when I try to playtest the game, Unity freezes/crashes. I'm not sure what's wrong with it so I'm not sure what to fix :/
Would it be better to make this a coroutine? Use mathf.lerp? What should I do? (NOTE: While it's flashing, it should still be able to read the rest of the code in my update function. Hitting wasd is what ends the flashing)
Thanks for any help :)
EDIT: So this is my current code: http://pastebin.com/7Nh0thL6 -- hopefully it should be quick/easy to understand!
My problem is that no matter what I set waitTime to, it still updates the alpha every frame rather than waiting after it updates. I want an entire cycle to take 0.75sec but currently it's entirely dependent on FPS and happens way too quickly
When a "while" loop is encountered, it will loop indefinitely until the condition is false. Therefore, if you are testing a conditional in a while loop, you must change the conditional as part of that while loop, or the while loop will never exit. This will cause Unity to freeze and ultimately crash.
Since you want the value to change over time, it actually doesn't make sense to update the alpha in a while loop because the entire while loop will execute in a single game frame. It will appear that you get to the target alpha immediately.
You could certainly use a coroutine for this:
private IEnumerator AlphaCoroutine(float targetAlpha)
{
while(color.a < targetAlpha)
{
color.a += someVal;
gameObject.renderer.material.color = color;
yield return null;
}
}
The yield statement causes one while loop block to run per frame. Also note that code only works for increasing alpha; you'd have to make some changes to remove alpha as well.
You can also use the Update() function with no while loop; just update the alpha up to your target alpha each update loop, and set the material color.
makes sense, ty :) Currently it's not showing any deviation in the alpha values, probably because it's being called once every frame. How can I make it update the alpha every 0.1 seconds, regardless of the fps? I tried adding a waitForSeconds at the end of the coroutine but that didnt help
Answer by Meltdown · Jul 18, 2013 at 02:11 AM
Don't use a while loop. Use Mathf.MoveTowards, which changes a value over time to a target value...
Color tempcolor = gameobject.renderer.material.color
tempcolor.a = Mathf.MoveTowards(0, 1, Time.deltaTime);
gameobject.renderer.material.color = tempcolor;
thanks for the help! This makes the code much simpler. Now I just have:
if(lastDirection == 'n')
{
Color tempColor = cubee.renderer.material.color;
if(transparencyGoingUp)
{
tempColor.a = $$anonymous$$athf.$$anonymous$$oveTowards(0.1f, 1f, Time.deltaTime);
transparencyGoingUp = false;
}
else //if it's going down
{
tempColor.a = $$anonymous$$athf.$$anonymous$$oveTowards(1f, 0.1f, Time.deltaTime);
transparencyGoingUp = true;
}
cubee.renderer.material.color = tempColor;
}
However it's still not changing everything, probably because it's being called every frame. How can I get the change in one direction (0.1 -> 1 or 1 -> 0.1) to happen over 0.75sec?
I liked the method since is fairly easy to implement and works in both directions but basically the problem in here is that for it to work you need to make sure you specify the actual variable value to be subtracted/incremented as one of the parameters, otherwise it will not work as expected since if you don't then it will increment/subtract any fixed amount you specify only once (even if its in a loop). So it needed be something like this:
... Class body ...
CanvasGroup theTree;
void Start ()
{
theTree = GetComponent<CanvasGroup>();
}
... coroutine body ...
float stepVal = 0.004f; // the amount to add or substract in time
while (theTree.alpha > 0)
{
theTree.alpha = $$anonymous$$athf.$$anonymous$$oveTowards(theTree.alpha, 0, stepVal);
yield return null;
}
... coroutine body ...
I tried this and it worked just fine, note that you need to play around guessing a bit in order to find the ideal target step values so you can get results that works fine for your case scenario. Note that in my example theTree is a CanvasGroup component added to the UI element of a Canvas (from the recently added UGUI features) which has an alpha value element by default and that will fade all of its child objects automatically for you. Hope it be of help.
Answer by Anymeese · Jul 21, 2013 at 07:40 AM
So.. This is my current code: http://pastebin.com/7Nh0thL6 -- hopefully it's very quick/easy to understand :)
My only problem is no matter what I set my timer variable (waitTime), the flashing happens incredibly quickly, as if the alpha is being incremented/decremented every frame. How do I space it out over a certain amount of time (roughly 0.75sec)?
Your answer
Follow this Question
Related Questions
ingame transparency 2 Answers
Transparent bumped specular 0 Answers
lerp transparency 2 Answers
Adding Transparency to a Diffuse / Spec / Normal Shader 0 Answers
Half transparent black circle 1 Answer