Unity2D如何使播放器根据其精灵的颜色与平台进行交互?

问题描述

首次发布者-长时间的浏览器。

我正在创建一个系统,其中玩家只能与颜色与其画笔(子精灵)相同的对象碰撞。随机设置对象的颜色和字符串时,该系统运行良好,但是当我尝试使用UI按钮实现该系统时,它只会忽略字符串更改。

    //VAR: Character Color
[Header("VAR Color Switch")]
public string currentColor;
public Color colorPrimary;
public Color colorSecondary;
public Color colorTertiary;
private GameObject child;
public SpriteRenderer sr;


private void Start() {
    child = transform.GetChild(0).gameObject; 
    sr = child.GetComponent<SpriteRenderer>(); 

    SetRandomColor(); 

}

private void OnCollisionEnter2D(Collision2D other) {
    switch(other.gameObject.tag)
    {
        case "Jumppad": 
            jumpForce = jumpBoost;
        break;
        case "Ground": 
            jumpForce = initialJumpForce;
        break;
        case "Primary":
            if(other.gameObject.tag == currentColor)
            {
                other.gameObject.GetComponent<Collider2D>().enabled = true;
            }else if (other.gameObject.tag != currentColor)
            {
                other.gameObject.GetComponent<Collider2D>().enabled = false;
            }
        break;
        case "Secondary":
            if(other.gameObject.tag == currentColor)
            {
                other.gameObject.GetComponent<Collider2D>().enabled = true;
            }else if (other.gameObject.tag != currentColor)
            {
                other.gameObject.GetComponent<Collider2D>().enabled = false;
            }
        break;
        case "Tertiary":
            if(other.gameObject.tag == currentColor)
            {
                other.gameObject.GetComponent<Collider2D>().enabled = true;
            }else if (other.gameObject.tag != currentColor)
            {
                other.gameObject.GetComponent<Collider2D>().enabled = false;
            }
        break;
    }
}


public void SetRandomColor()
{
    int index = UnityEngine.Random.Range(0,3);

    switch (index)
    {
        case 0:
            currentColor = "Primary";
            sr.color = colorPrimary;
        break;
        case 1:
            currentColor = "Secondary";
            sr.color = colorSecondary;
        break;
        case 2:
            currentColor = "Tertiary";
            sr.color = colorTertiary;
        break;
    }
}

}

UI按钮上的代码

 PlayerController player;

// Start is called before the first frame update
void Start()
{
    player = FindobjectOfType<PlayerController>();
}

public void SwitchPrimary()
{
    player.currentColor = "Primary";
    player.sr.color = player.colorPrimary;
}

public void SwitchSecondary()
{
    player.currentColor = "Secondary";
    player.sr.color = player.colorSecondary;
}

public void SwitchTertiary()
{
    player.currentColor = "Teritary";
    player.sr.color = player.colorTertiary;
}

我真的不确定可能是什么问题。无论我尝试什么变化,我都能以相同的方式得到结果。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)