简体   繁体   English

更改透明纹理的不透明度

[英]Changing the opacity of a transparent texture

I have a transparent texture of a chain link fence. 我有链节围栏的透明纹理。 I want the fence to fade in as the player approaches from the z direction. 我希望栅栏随着玩家从z方向接近而逐渐消失。 The problem I am having is that because the fence is transparent the opacity slider disappears and uses the image transparency. 我遇到的问题是,因为围栏是透明的,所以不透明度滑块消失并使用图像透明度。 (I want the transparent texture to fade in) My current code: (我希望透明纹理淡入)我当前的代码:

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
    }

  }

The fence never fades or disappears when wallonedist is > 10 当wallonedist> 10时,围栏永远不会消失或消失

Color is a struct which means that changing it won't change the instance of of the Renderer . Color是一种struct ,这意味着更改它不会更改Renderer的实例。 It is a copy of a color from the Renderer . 它是Renderer color的副本。 If you change the color, you have to re-assign the whole color back to the Renderer for it to take effect. 如果更改颜色,则必须将整个颜色重新分配给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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM