当多个对撞机同时触摸我的角色时,如何首先查找具有相同标签的对撞机

时间:2019-07-02 18:21:40

标签: c# unity3d touch

我试图找出当多个对撞机同时触摸我的角色时,哪个具有相同标签的对撞机首先接触我的角色。

if (col.transform.gameObject.tag == "enemy") {
            hit = true;
            rgd.AddForce(new Vector2(0, 2.150f), ForceMode2D.Impulse);
            if (transform.position.x-col.transform.gameObject.transform.position.x < 0)
            {
                rgd.AddForce(-1*rgd.transform.right * 40);
            }
            else
            {
                rgd.AddForce(1*rgd.transform.right * 40);
            }

            if (Mathf.Approximately (angle, 0)) {
                Destroy (col.transform.gameObject);
                damage = 0;
            } else {
                damage = 25;
            }
            enemydamageSound.Play();
        }

1 个答案:

答案 0 :(得分:1)

我通过添加一些代码解决了它。这是我的新代码:

private int jumpCount = 0;

if (col.transform.gameObject.tag == "ground")
        {
            isGround = true;
            jumpped = true;
            jumpCount = 0;
        }

if (col.transform.gameObject.tag == "enemy")
    {
        hit = true;
        if (!isGround && jumpCount == 0)
        {
            rgd.AddForce(new Vector2(0, 2.150f), ForceMode2D.Impulse);
            jumpCount++;
        }
        if (transform.position.x - col.transform.gameObject.transform.position.x < 0)
        {
            if (jumpCount == 0)
            {
                rgd.AddForce(new Vector2(0, 2f), ForceMode2D.Impulse);
            }
            rgd.AddForce(-1 * rgd.transform.right * 40);
        }
        else
        {
            if (jumpCount == 0)
            {
                rgd.AddForce(new Vector2(0, 2f), ForceMode2D.Impulse);
            }
            rgd.AddForce(1 * rgd.transform.right * 40);
        }

        if (Mathf.Approximately(angle, 0))
        {
            Destroy(col.transform.gameObject);
            damage = 0;
        }
        else
        {
            damage = 25;
        }
        enemydamageSound.Play();
    }