更改透明纹理的不透明度

时间:2016-06-15 13:13:44

标签: c# unity3d

我有链条围栏的透明纹理。当玩家从z方向靠近时,我希望栅栏渐渐消失。我遇到的问题是因为栅栏是透明的,不透明度滑块消失并使用图像透明度。 (我希望透明纹理淡入)我目前的代码:

public class WallFader : MonoBehaviour {


public GameObject wallone;

private Vector3 wallonetransform;

private Color wallonecolor;


public GameObject player;
private Vector3 playerposition;

private float PPX;
private float PPZ;

// Use this for initialization
void Start()

{
    wallonetransform = wallone.GetComponent<Transform>().position;
    wallonecolor = wallone.GetComponent<Renderer>().material.color;

}

// Update is called once per frame
void Update () {


    playerposition = player.transform.position;
    PPX = playerposition.x;
    PPZ = playerposition.z;

    // Distance to the large flat wall
    float wallonedist = wallonetransform.z - PPZ;

    if (wallonedist > 10)
    {
        wallonecolor.a = 0;
    }

    else
    {
        //fade in script
    }

  }

当Wallonedist为&gt;时,栅栏永不褪色或消失。 10

1 个答案:

答案 0 :(得分:1)

Colorstruct,这意味着更改它不会更改Renderer的实例。它是colorRenderer的副本。如果更改颜色,则必须将整个颜色重新分配回Renderer才能生效。

public class WallFader : MonoBehaviour
{
    public GameObject wallone;

    private Vector3 wallonetransform;

    private Color wallonecolor;
    Renderer renderer;

    public GameObject player;
    private Vector3 playerposition;

    private float PPX;
    private float PPZ;

    // Use this for initialization
    void Start()
    {
        wallonetransform = wallone.GetComponent<Transform>().position;
        renderer = wallone.GetComponent<Renderer>();
        wallonecolor = wallone.GetComponent<Renderer>().material.color;
    }

    // Update is called once per frame
    void Update()
    {


        playerposition = player.transform.position;
        PPX = playerposition.x;
        PPZ = playerposition.z;

        // Distance to the large flat wall
        float wallonedist = wallonetransform.z - PPZ;

        if (wallonedist > 10)
        {
            wallonecolor.a = 0;
            renderer.material.color = wallonecolor; //Apply the color
        }

        else
        {
            //fade in script
        }
    }
}