代码之家  ›  专栏  ›  技术社区  ›  Siddharth

逐渐减少滑痕α

  •  0
  • Siddharth  · 技术社区  · 6 年前

    在我的汽车战争游戏中,我在汽车转弯时产生了打滑痕迹。 它工作正常,几秒钟后,我想减少它的alpha值并将其从游戏中移除。我用轨迹渲染器生成了滑痕。

    • 我需要为材质指定什么材质球?
    • 我怎样才能降低它的α?

    目前,我已经使用了这种轨迹渲染器材质:

    enter image description here

    现在为了逐渐减少alpha,我有这样的代码:

    private IEnumerator Start()
    {
        Material myMaterial = GetComponent<Renderer>().material;
    
        while (true)
        {
            yield return new WaitForSeconds(1f);
    
            // check whether this skid trail has finished
            // (the Wheel script sets the parent to null when the skid finishes)
            if (transform.parent.name == "SkidTrailsDetachedParent")
            {
    
                // set the start colour
                //Color startCol = GetComponent<Renderer>().material.color;
                //Color startCol = myMaterial.GetColor("_EmisColor");
                Color startCol = myMaterial.GetColor("_TintColor");
    
                // wait for the persist time
                yield return new WaitForSeconds(persistTime);
    
                float t = Time.time;
    
                // fade out the skid mark
                while (Time.time < t + fadeDuration)
                {
                    float i = Mathf.InverseLerp(t, t + fadeDuration, Time.time);
                    //myMaterial.color = startCol * new Color(1, 1, 1, 1 - i);
                    //myMaterial.SetColor("_EmisColor", startCol * new Color(1f, 1f, 1f, 1f - i));
                    myMaterial.SetColor("_TintColor", startCol * new Color(1f, 1f, 1f, 1f - i));
                    yield return null;
                }
    
                // the object has faded and is now done so destroy it
                Destroy(gameObject);
            }
        }
    }
    

    不过,这对我不起作用。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Eliasar    6 年前

    TrailRenderer 有一个名为 endColor 你可以设置 Color 这样这条线就会消失到最后。

    void Start()
    {
        Color noAlphaEndColor = line.endColor;
        line.endColor = new Color(noAlphaEndColor.r, noAlphaEndColor.g, noAlphaEndColor.b, 0.0f);
    }
    
    void Update()
    {
        if (line.endColor.a < 0.001)
        {
            Destroy(gameObject);
        }
    }