Search Unity

LineRenderer Fade Out (Solved)

Discussion in 'Scripting' started by Salade_de_poney, Jul 30, 2018.

  1. Salade_de_poney

    Salade_de_poney

    Joined:
    Jan 20, 2017
    Posts:
    60
    Hello Everyone,

    I have a line renderer with 2 materials in it. I would love to fade it out when necessary.

    I tried to change the alpha of the material's colour but I couldn't succeed. Is there a way to directly touch the alpha of the gradient points ?

    I also tried to create a new Material using :
    Code (CSharp):
    1.     private Material newMat;
    2. private Material newMat2;
    3.  
    4.     private void Start()
    5.     {
    6.         lineRenderer = GetComponent<LineRenderer>();
    7.         StartCoroutine(Beam());
    8.     }
    9.  
    10.     private IEnumerator Beam()
    11.     {
    12.         yield return customFrame;
    13.         FrameNumber += 1;
    14.         if (FrameNumber < 5)
    15.             StartCoroutine(Beam());
    16.         else
    17.         {
    18.             newMat = new Material(Shader.Find("Particle/Additive"));
    19.             newMat2 = new Material(Shader.Find("Particle/Additive"));  
    20.             newMat.mainTexture = ElectricShape;
    21.             newMat2.mainTexture = ElectricShape2;
    22.             lineRenderer.materials[0] = newMat;
    23.             lineRenderer.materials[1] = newMat2;
    24.             fadeOut = true;
    25.         }
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         if(fadeOut)
    31.         {
    32.             alpha -= Time.deltaTime * fadeOutSpeed;
    33.             lineRenderer.materials[0].SetColor("_TintColor", new Color(newMat.color.r,newMat.color.g, newMat.color.b ,alpha));
    34.             lineRenderer.materials[1].SetColor("_TintColor", new Color(newMat.color.r,newMat.color.g, newMat.color.b ,alpha));
    35.         }
    36.     }
    The problem is that I lose the "Shape" of the material... and the fade out doesn't work.

    I am afraid that if I don't cree a new material, it will change the colour of all the object instantiated with the same material.

    Thank you very much !

    Screen Shot 2018-07-30 at 16.03.58.png
     
    Last edited: Jul 30, 2018
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. Salade_de_poney

    Salade_de_poney

    Joined:
    Jan 20, 2017
    Posts:
    60
    Thank you !

    I just solved the problem using :

    Code (CSharp):
    1.            fadeOutSpeed += Time.deltaTime;
    2.             Color m_color = Color.Lerp(new Color(0.5f, 0.5f, 0.5f , 0.5f), new Color(0f, 0f, 0f, 0f), fadeOutSpeed);
    3.             Debug.Log(m_color);
    4.             lineRenderer.materials[0].SetColor("_TintColor", m_color);
    5.             lineRenderer.materials[1].SetColor("_TintColor", m_color);
    I'll use your solution next time.
     
    Xriuk and BitGamey like this.
  4. zhaorongtuzt7

    zhaorongtuzt7

    Joined:
    May 3, 2020
    Posts:
    1
    could you explain this to me, i understand how to fadeout a spriterenderer using materials but i don't understand how this works. thx in advance c:
     
  5. Logophile

    Logophile

    Joined:
    Feb 2, 2019
    Posts:
    1
    Kind of a nerco I know, but if anyone wants an approach that uses colour gradients this is what I came up with:

    Code (CSharp):
    1. IEnumerator FadeLineRenderer ()
    2.    {
    3.         Gradient lineRendererGradient = new Gradient();
    4.         float fadeSpeed = 3f;
    5.         float timeElapsed = 0f;
    6.         float alpha = 1f;
    7.  
    8.         while (timeElapsed < fadeSpeed)
    9.         {
    10.             alpha = Mathf.Lerp(1f, 0f, timeElapsed / fadeSpeed);
    11.  
    12.             lineRendererGradient.SetKeys
    13.             (
    14.                 lineRenderer.colorGradient.colorKeys,
    15.                 new GradientAlphaKey[] { new GradientAlphaKey(alpha, 1f) }
    16.             );
    17.             lineRenderer.colorGradient = lineRendererGradient;
    18.  
    19.             timeElapsed += Time.deltaTime;
    20.             yield return null;
    21.         }
    22.  
    23.         Destroy(gameObject);
    24.     }
    - lineRenderer.colorGradient.colorKeys gets the current linerenderer gradient (ie, the one you've set in the inspector).
    - If you don't want to destroy the linerenderer gameobject once its faded out you should set the gradients alpha value to 0 after the while loop instead.