检测与三个物体的碰撞

Detect Collisions with Three Objects

我有一个要在关卡中移动的方块,我想让它在碰到一个角(例如:右上边界)时不会移动到任何地方。常规碰撞检测不起作用,因为我让方块以短距离移动(player.transform.position.x - 1 等)。

这是我使用的代码:

public void OnCollisionEnter2D(Collision2D collision)
{ if (collision.gameObject.tag == "barrier" && (GetComponent<BoxCollider2D>().tag == "top" && GetComponent<BoxCollider2D>().tag == "left"))
    {
        player.canMoveUp = false;
        player.canMoveDown = true;
        player.canMoveLeft = false;
        player.canMoveRight = true;
    }
    else if (collision.gameObject.tag == "barrier" && (GetComponent<BoxCollider2D>().tag == "top" && GetComponent<BoxCollider2D>().tag == "right"))
    {
        player.canMoveUp = false;
        player.canMoveDown = true;
        player.canMoveLeft = true;
        player.canMoveRight = false;
    }
    else if (collision.gameObject.tag == "barrier" && (GetComponent<BoxCollider2D>().tag == "bottom" && GetComponent<BoxCollider2D>().tag == "left"))
    {
        player.canMoveUp = true;
        player.canMoveDown = false;
        player.canMoveLeft = false;
        player.canMoveRight = true;
    }
    else if (collision.gameObject.tag == "barrier" && (GetComponent<BoxCollider2D>().tag == "bottom" && GetComponent<BoxCollider2D>().tag == "right"))
    {
        player.canMoveUp = true;
        player.canMoveDown = false;
        player.canMoveLeft = true;
        player.canMoveRight = false;
    }
}

public void OnCollisionExit2D(Collision2D collision)
{
    Debug.Log("notTouching");
    player.canMoveUp = true;
    player.canMoveDown = true;
    player.canMoveLeft = true;
    player.canMoveRight = true;
}

编辑:新代码。

 if (Input.GetKeyUp(KeyCode.LeftArrow))
    {
        Vector2 movementLeft = new Vector2(-1, 0);
        RaycastHit2D hitLeft = Physics2D.Raycast(transform.position, movementLeft, -1);

        if (hitLeft.collider && hitLeft.collider.gameObject.tag == "barrier")
        {
            Debug.Log("N/A");
        }
        else
        {
            player.transform.position += new Vector3(movementLeft.x,movementLeft.y,0);
        }
    }
    else if (Input.GetKeyUp(KeyCode.RightArrow))
    {
        Vector2 movementRight = new Vector2(1, 0);
        RaycastHit2D hitRight = Physics2D.Raycast(transform.position, movementRight, 1);

        if (hitRight.collider && hitRight.collider.gameObject.tag == "barrier")
        {
            Debug.Log("N/A");
        }
        else
        {
            player.transform.position += new Vector3(movementRight.x, movementRight.y, 0);
        }
    }
    else if (Input.GetKeyUp(KeyCode.DownArrow))
    {
        Vector2 movementDown = new Vector2(0, -1);
        RaycastHit2D hitDown = Physics2D.Raycast(transform.position, movementDown, -1);

        if (hitDown.collider && hitDown.collider.gameObject.tag == "barrier")
        {
            Debug.Log("N/A");
        }
        else
        {
            player.transform.position += new Vector3(movementDown.x, movementDown.y, 0);
        }
    }
    else if (Input.GetKeyUp(KeyCode.UpArrow))
    {
        Vector2 movementUp = new Vector2(0, 1);
        RaycastHit2D hitUp = Physics2D.Raycast(transform.position, movementUp, 1);

        if (hitUp.collider && hitUp.collider.gameObject.tag == "barrier")
        {
            Debug.Log("N/A");
        }
        else
        {
            player.transform.position += new Vector3(movementUp.x, movementUp.y, 0);
        }
    }
    else
    {
        Debug.Log("N/A");
    }

一个更简单的解决方案是使用 Physics.Raycast2D 在移动之前检查是否有任何障碍物阻碍您的移动。

这是代码的大概思路:

// Some code to get input and end up with a movement vector
...
movement = new Vector2(1, 0);

// Raycast in the direction of movement with a distance of 1.
RaycastHit2D hit = Physics2D.Raycast(transform.position, movement, 1);

// We hit something and its a barrier
if (hit.collider && hit.collider.gameobject.tag == "barrier")
{
    // Don't move, play sound that indicate you hit a barrier or some sort
}
else
{
    // Move to the new position
    transform.position += new Vector3(movement.x, movement.y, 0);
}