Search Unity

Reduce Skid Mark Alpha Gradually

Discussion in 'Shaders' started by siddharth3322, Feb 13, 2019.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    For my car war game, I have generated skid marks as the car takes a turn. It's working properly, now after a few seconds passed, I want to reduce its alpha and remove it from the game. I have used trail renderer to generate skid marks.

    • what shader do I require to assign to the material?
    • which way I can reduce its alpha?

    At present, I have used this kind of trail renderer material:
    skid_mark_material.png

    Now for reducing alpha gradually, I have this kind of code:
    Code (CSharp):
    1. private IEnumerator Start()
    2. {
    3.     Material myMaterial = GetComponent<Renderer>().material;
    4.  
    5.     while (true)
    6.     {
    7.         yield return new WaitForSeconds(1f);
    8.  
    9.         // check whether this skid trail has finished
    10.         // (the Wheel script sets the parent to null when the skid finishes)
    11.         if (transform.parent.name == "SkidTrailsDetachedParent")
    12.         {
    13.  
    14.             // set the start colour
    15.             //Color startCol = GetComponent<Renderer>().material.color;
    16.             //Color startCol = myMaterial.GetColor("_EmisColor");
    17.             Color startCol = myMaterial.GetColor("_TintColor");
    18.  
    19.             // wait for the persist time
    20.             yield return new WaitForSeconds(persistTime);
    21.  
    22.             float t = Time.time;
    23.  
    24.             // fade out the skid mark
    25.             while (Time.time < t + fadeDuration)
    26.             {
    27.                 float i = Mathf.InverseLerp(t, t + fadeDuration, Time.time);
    28.                 //myMaterial.color = startCol * new Color(1, 1, 1, 1 - i);
    29.                 //myMaterial.SetColor("_EmisColor", startCol * new Color(1f, 1f, 1f, 1f - i));
    30.                 myMaterial.SetColor("_TintColor", startCol * new Color(1f, 1f, 1f, 1f - i));
    31.                 yield return null;
    32.             }
    33.  
    34.             // the object has faded and is now done so destroy it
    35.             Destroy(gameObject);
    36.         }
    37.     }
    38. }
    Still, it's not working for me so I expect some help from your side.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Most particle shaders use the mesh's vertex color for things like this. If you're already using a trail renderer, you could modify the color gradient values from script to fade it out.
     
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    do you mean I require to reduce alpha of trail renderer?
    Rather than its material....
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352