简体   繁体   English

检测碰撞时如何改变物体的“光环”?

[英]How change “halo” in a object when detect collision?

I have an object that changes its color when it collides with another object, and decreases its size: gameObject.transform.localScale /= 2;我有一个对象,当它与另一个对象碰撞时会改变它的颜色,并减小它的大小: gameObject.transform.localScale /= 2; but it has a white halo .但它有一个白色的halo

I want the halo to match my object's color.我希望halo与对象的颜色相匹配。 So when the object is a green color, so also the halo will be green as well.所以当物体是绿色时, halo也会是绿色的。 And if my object is blue, then the halo will be blue as well.如果我的物体是蓝色的,那么halo也会是蓝色的。 Also, I want that when my object detect collision with other object, halo also decrease and I don't know how can I do it.另外,我希望当我的物体检测到与其他物体的碰撞时, halo也会减少,我不知道该怎么做。

Code change color (blue, red or green) when I press the screen:当我按下屏幕时,代码会改变颜色(蓝色、红色或绿色):

public class ChangeColor : MonoBehaviour {

    public Material[] materials;
    public Renderer rend;

    private int index = 1;

    // Use this for initialization
    void Start () {

        rend = GetComponent<Renderer> ();
        rend.enabled = true;

    }

    public void Update() {
        if (materials.Length == 0) {
            return;
        }
        if (Input.GetMouseButtonDown (0)) {
            index += 1;

            if (index == materials.Length + 1) {
                index = 1; 
            }
            print (index);

            rend.sharedMaterial = materials [index - 1];                        
        }
    }
}

I know use halo but programatically I don't know.我知道使用halo但以编程方式我不知道。

This Halo Component is accessible with a bit of work, let me demonstrate it with code.这个 Halo 组件可以通过一些工作来访问,让我用代码来演示它。

Private void Start() {   
    SerializedObject haloComponent = new SerializedObject(this.gameObject.GetComponent("Halo"));
    haloComponent?.FindProperty("m_Color").colorValue = Color.Red;
}

There are some more things you can do but this is the way to get a reference to the Halo.您还可以执行更多操作,但这是获取 Halo 引用的方法。 Keep in mind GetComponent<> is searching for something that is Halo and GetComponent("Halo") is searching for something with the name Halo.请记住, GetComponent<>正在搜索名为 Halo 的内容,而GetComponent("Halo")正在搜索名为 Halo 的内容。 Since the Component is named Halo it works like a charm.由于组件被命名为 Halo,它的作用就像一个魅力。 Try it out试试看

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

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