- Home /
How to animate disappearing Object?
I want a GameObject like the cube do fade in or fade out when I want it to. With fading in and out I mean that the Object seems to disappear and slowly becomes invisible.
I know that if I want to make an Object invisible I can do gameObject.SetActive(false). But I want it to be animated. I also tried changing the Renderers alph over time. That works but doesn´t look good. And even if the object is fully transparent you can still see it.
So I thought about creating a transparent wall that somehow hides the Object. So that you don´t see the wall and you don´t see the gameObject. I don´t know if this is even possible.
Does anyone have a better aproach about doing this? Or do you have any idea on how I would realize my idea? Thanks in advance!
Answer by look001 · Jul 28, 2017 at 01:41 PM
you need shaders for this.
Shader "Custom/Fade" {
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_Visibility ("Visibility", Range(0,1)) = 1
}
SubShader
{
Tags {"Queue" = "Transparent" "RenderType"="Transparent" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows alpha:fade
#pragma target 3.0
sampler2D _MainTex;
float _Visibility;
struct Input {
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = _Visibility;
}
ENDCG
}
FallBack "Standard"
}
This is a basic Standard Surface Shader. I modified a bit so you can change the visibitily. All you need to do is make a script that changes the _Visibility value.
bool FadeIn() {
float val = Mathf.Lerp (0, 1, t);
t += speed * Time.deltaTime;
renderer.material.SetFloat ("_Visibility", val);
return t > 1;
}
If the Fade is done the method returns true. After every fade you need to reset the variable t to 0. Good Luck!
Thank you so much!!! This is working exactly as I imagined!
Answer by · Jul 28, 2017 at 01:36 PM
You need to create your own shader if ou want it to be perfect. Plugins like Amplify Shader and Shader Forge can help you create it.
Answer by K0ST4S · Dec 21, 2018 at 10:29 AM
If you have SpriteRenderer, you can create animation where you change alfa value of color from 1 to 0 over time.
Your answer
![](https://koobas.hobune.stream/wayback/20220612131403im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Keyframes of animated Child gameobject not shown by parent gameobjects animator issue ? 0 Answers
Objects in Hierarchy Not Visible Anywhere 0 Answers
Does the Singleton GameObject need to be in all scenes where i need to access it? 2 Answers
Randomly genrerated movement not working. 1 Answer
GameObject as a toggle button to play character Animation 1 Answer