- Home /
Animation to fade alpha but retain color
Is it possible to use an animation to fade the alpha channel on a material without affecting the colours?
I can remove the properties for R G B but as soon as I remove the A, the entire color property is removed.
I want to use this animation to fade out multiple objects of different colours, but they all inherit the colour of the animation.
I could do this via script, but was wondering if there is a way to do this using animation.
I am a bit confused with your question, do you want to keep the rgb values of your object and change the alpha? or are you trying to change the rgb values and the alpha in the animation? If you are trying to change the alpha and rgb values of all your objects to the same value, wouldn't changing the rgba values already get the job done?
Answer by Serinx · Apr 17, 2018 at 06:43 AM
It appears that the animation cannot modify the existing color, it creates a new color and applies it to the object.
I decided to use a coroutine to fade out the alpha instead. Here's the code I used if anyone stumbles across this problem.
StartCoroutine(fadeAlpha()); // Add this line in some other function e.g. Start()
private IEnumerator fadeAlpha()
{
float duration = 1f;
float startTime = Time.time;
MeshRenderer[] renderers = gameObject.GetComponentsInChildren<MeshRenderer>();
while (Time.time < startTime + duration)
{
foreach (MeshRenderer rend in renderers)
{
Color clr = rend.material.color;
Color newColor = new Color(clr.r, clr.g, clr.b, clr.a - (Time.deltaTime / duration));
rend.material.SetColor("_Color", newColor);
}
yield return null;
}
}
Answer by LandonC · Apr 16, 2018 at 04:23 AM
If you are using the Animator component, just animate the color.a value without affecting the r, g, b values.
When I record and change the alpha value in the inspector, it adds a property for all of the rgb values. I can remove 2 of them e.g. r and g but as soon as i remove the third (b) it also removes the alpha property.
AH I see. I tried myself and was surprised! I have been using Animator component heavily and never had I made this happened.
Try this. When the record animation button is toggled, only change the alpha bar, the black to white gradient bar on the color panel. DO NOT SLIDE ON THE COLOR PIC$$anonymous$$ER, just the bar. You will get the alpha values keyed ins$$anonymous$$d of the RGB values.
I managed to get only the alpha channel to display in the properties but now when it animates it is black, regardless of the initial colour of the object.
There are 2 objects. Ignore the emission - this has no impact on the issue.
Answer by cobertos · May 04, 2020 at 12:28 AM
If you're using a custom shader, you might be able to add your own extra Vector1 property and just animate that instead
Answer by petremlabs · Jan 18, 2021 at 10:54 AM
You may try this:
In the Animation window, in the line where you change the Sprite Renderer Color property (if you're using Sprites), expand the property, and click on the small icon on the right of the rgb lines > Remove Property.
I think it should solve the issue.
Your answer
Follow this Question
Related Questions
Can I make animations snap to a frame? 1 Answer
Button image sprite colour change 1 Answer
Partially recolor 2D sprites on either side of a line 0 Answers
Material color won't update! 2 Answers