改变透明纹理的不透明度



我具有链链围栏的透明纹理。我希望随着玩家从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是> 10

时,篱笆永远不会褪色或消失

Colorstruct,这意味着更改它不会更改Renderer的实例。它是Renderercolor的副本。如果更改颜色,则必须将整个颜色重新分配回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
        }
    }
}

最新更新