简体   繁体   English

Unity:使MeshRenderer的颜色透明

[英]Unity: Make color of MeshRenderer transparent

I have a little question.我有一个小问题。 I have a cube which has a Mesh Renderer and I gave it a default black material as its color.我有一个立方体,它有一个网格渲染器,我给它一个默认的黑色材质作为它的颜色。 Now I would like to make the object more transparent when it touches something.现在我想让 object 在接触某些东西时更加透明。 I already set the Rendering Mode to Fade or Transparent.我已经将渲染模式设置为淡入淡出或透明。 The tag is also set.标签也已设置。 This is my code:这是我的代码:

public GameObject cube; 
private Color tempcolor; 
void Start()
{
    tempcolor = cube.GetComponent<MeshRenderer>().material.color;   
}
public void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Object"))
    {
        tempcolor.a -= 0.1f; 
    }
}

My code doesn't do the job.我的代码没有完成这项工作。 When I Debug.Log tempcolor.a it goes down but nothing happens with the cube.当我 Debug.Log tempcolor.a 它下降但立方体没有任何反应。 I also tried normal Renderer but it didn't work.我也尝试过普通的渲染器,但它没有用。 Any idea?任何的想法?

I would be grateful if you can help me out如果你能帮助我,我将不胜感激

Kind regards亲切的问候

You don't need tempColor and Start() event, just save the main component temporary.您不需要tempColorStart()事件,只需临时保存主要组件。

public void OnTriggerEnter(Collider other)
{
    var meshRenderer = cube.GetComponent<MeshRenderer>();
    
    if (other.CompareTag("Object"))
    {
        var color = meshRenderer.material.color;
        color.a -= .1f;
        meshRenderer.material.color = color;
    }
}

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

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