[英]Changing the opacity of a transparent texture
我有鏈節圍欄的透明紋理。 我希望柵欄隨着玩家從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時,圍欄永遠不會消失或消失
Color
是一種struct
,這意味着更改它不會更改Renderer
的實例。 它是Renderer
color
的副本。 如果更改顏色,則必須將整個顏色重新分配給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.