У меня есть блок, который я перемещаю по уровню, и я хочу сделать его таким, чтобы, если он попадает в угол (например, верхняя правая граница), он никуда не двигался.Обычное обнаружение столкновений не работает, потому что у меня движение блока в коротких прыжках (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");
}