Search Unity

Question How can I fade out/in multiple materials ?

Discussion in 'Scripting' started by SharonL75, Oct 15, 2020.

  1. SharonL75

    SharonL75

    Joined:
    Aug 13, 2020
    Posts:
    91
    First for testing I just want to fade out the materials I have 6 materials.

    First material name is Droid and it appears 4 times in 4 objects so maybe I need to fade this material out only once ? I dragged the same material 4 timers to the array :




    The object spolySurface33 and 35 and 34 and 34 tll this four objects using the same material Droid.

    Then there are more two objects droid_glass and droid_eyes that use their own materials.

    The second problem is that I tried to change this materials the Droid and the Eye and the Glass from Opaque to Fade and then to Transparent but it didn't fade them out.

    This script is attached to the object I want to fade out :

    Something is wrong with the script it never fade out the transform even if the materials are set to Transparent or to Fade.


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MoveNavi : MonoBehaviour
    7. {
    8.    public GameObject rig_f_middle;
    9.    public float speed;
    10.    public float distanceFromTarget;
    11.    public static bool naviChildOfHand = false;
    12.    public GameObject naviParent;
    13.    public MeshRenderer[] materialsToFade;
    14.  
    15.    private List<Material> materials = new List<Material>();
    16.    private bool startedFade = false;
    17.  
    18.    private void Start()
    19.    {
    20.        foreach (MeshRenderer meshrend in materialsToFade)
    21.        {
    22.            materials.Add(meshrend.material);
    23.        }
    24.    }
    25.  
    26.    private void Update()
    27.    {
    28.        if (IKControl.startMovingNAVI == true && startedFade == false)
    29.        {
    30.            var v = rig_f_middle.transform.position - transform.position;
    31.            if (v.magnitude < distanceFromTarget)
    32.            {
    33.                naviChildOfHand = true;
    34.                StartCoroutine(FadeTo(materials, 0, 5f));
    35.                startedFade = true;
    36.                return;
    37.            }
    38.            Vector3 moveDir = v.normalized;
    39.            transform.position += moveDir * speed * Time.deltaTime;
    40.        }
    41.  
    42.    }
    43.  
    44.    IEnumerator FadeTo(List<Material> materials, float targetOpacity, float duration)
    45.    {
    46.        foreach (Material mat in materials)
    47.        {
    48.            // Cache the current color of the material, and its initiql opacity.
    49.            Color color = mat.color;
    50.            float startOpacity = color.a;
    51.  
    52.            // Track how many seconds we've been fading.
    53.            float t = 0;
    54.  
    55.            while (t < duration)
    56.            {
    57.                // Step the fade forward one frame.
    58.                t += Time.deltaTime;
    59.                // Turn the time into an interpolation factor between 0 and 1.
    60.                float blend = Mathf.Clamp01(t / duration);
    61.  
    62.                // Blend to the corresponding opacity between start & target.
    63.                color.a = Mathf.Lerp(startOpacity, targetOpacity, blend);
    64.  
    65.                // Apply the resulting color to the material.
    66.                mat.color = color;
    67.  
    68.                // Wait one frame, and repeat.
    69.                yield return null;
    70.            }
    71.        }
    72.    }
    73. }
    74.