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

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();
        }

最佳答案

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

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();
    }

关于c# - 当多个对撞机同时触摸我的角色时,如何首先找到具有相同标签的对撞机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56858170/

10-09 19:41