Fading the transparency of a material

In this recipe, we will create an object that, once clicked, fades out and disappears. However, the script will be flexible enough to allow us adjust the initial and final alpha values. Plus, we will have the option of making the object self-destructible when turned invisible.

How to do it...

Follow these steps:

  1. Add a Sphere to your scene by accessing the GameObject | 3D Object | Sphere menu.
  2. Select the Sphere and make sure it has a collider (if you are using a custom 3D object, you might have to add a collider through the Components | Physics menu).
  3. Create a new material. The easiest way to do that is to access the Project view, click the Create drop-down menu, and choose Material.
  4. Rename your new material. For this example, let's call it Fade_MAT.
  5. Select your material. From the Inspector view, use the drop-down menu to change its Rendering Mode to Fade:
    How to do it...

    Tip

    The Fade rendering mode is specifically designed for situations like this. Other rendering modes, such as Transparent, will fade turn the Albedo color transparent, but not the specular highlights nor the reflections, in which case the object will still be visible.

  6. Apply the FadeMaterial to Sphere by dragging it from the Project view into the Sphere Game Object name in the Hierarchy view.
  7. From the Project view, click the Create drop down menu and choose C# Script. Rename it as FadeMaterial and open it in your editor.
  8. Replace your script with the code below:
    using UnityEngine;
    using System.Collections;
    public class FadeMaterial : MonoBehaviour {
      public float fadeDuration = 1.0f;
      public bool useMaterialAlpha = false;
      public float alphaStart = 1.0f;
      public float alphaEnd = 0.0f;
      public bool destroyInvisibleObject = true;
      private bool isFading = false;
      private float alphaDiff;
      private float startTime;
      private Renderer rend;
      private Color fadeColor;
    
      void Start () {
        rend = GetComponent<Renderer>();
        fadeColor = rend.material.color;
    
        if (!useMaterialAlpha) {
          fadeColor.a = alphaStart;
        } else {
          alphaStart = fadeColor.a;
        }
    
        rend.material.color = fadeColor;
        alphaDiff = alphaStart - alphaEnd;
      }
    
      void Update () {
        if(isFading){
          var elapsedTime = Time.time - startTime;
    
          if(elapsedTime <= fadeDuration){
            var fadeProgress = elapsedTime / fadeDuration;
            var alphaChange = fadeProgress * alphaDiff;
            fadeColor.a = alphaStart - alphaChange;
            rend.material.color = fadeColor;
            } else {
            fadeColor.a = alphaEnd;
            rend.material.color = fadeColor;
    
            if(destroyInvisibleObject)
              Destroy (gameObject);
            isFading = false;  
          }
        }
      }
    
      void OnMouseUp(){
        FadeAlpha();
      }
    
      public void FadeAlpha(){
        isFading = true;
        startTime = Time.time;
      }
    }
  9. Save your script and apply it to the Sphere Game.
  10. Play your scene and click on the Sphere to see it fade away and self-destruct.

How it works...

Since the opaqueness of the material using a transparent Shader is determined by the alpha value of its main color, all we need to do in order to fade it is changing that value over a given amount of time. This transformation is expressed, in our script, on the following lines of code:

var fadeProgress = elapsedTime / fadeDuration;
var alphaChange = fadeProgress * alphaDiff;
fadeColor.a = alphaStart - alphaChange;
rend.material.color = fadeColor;

There's more...

You could call the FadeAlpha function in other circumstances (such as a Rigidbody collision, for instance). In fact, you could even call it from another Game Object's script by using the GetComponent command. The script would be something like:

GameObject.Find("Sphere").GetComponent<FadeMaterial>().FadeAlpha();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.2.231