Я создал 3 игровых объекта groundCheck как дочерние элементы моего игрока Transform, и они расположены под ним: один внизу по центру, один внизу слева и один внизу справа. Затем я проверяю, сталкиваются ли они со стенами или землей с помощью этих функций:
Код: Выделить всё
bool CheckDown()
{
if (Physics2D.OverlapCircle(groundCheck.position, 0.01f, whatIsGround))
return true;
else
return false;
}
bool CheckGround()
{
if (Physics2D.OverlapCircle(groundCheck.position, 0.01f, whatIsGround))
return true;
if (Physics2D.OverlapCircle(groundCheck2.position, 0.01f, whatIsGround))
return true;
if (Physics2D.OverlapCircle(groundCheck3.position, 0.01f, whatIsGround))
return true;
else
return false;
}
bool CheckWallLeft()
{
if (Physics2D.OverlapCircle(groundCheck3.position, 0.01f, whatIsGround))
return true;
else
return false;
}
bool CheckWallRight()
{
if (Physics2D.OverlapCircle(groundCheck2.position, 0.01f, whatIsGround))
return true;
else
return false;
}
Код: Выделить всё
if (CheckWallLeft() && !CheckWallRight())
{
if (h > 0)
{
GetComponent().velocity = new Vector2(h * speedHeight, GetComponent().velocity.y);
}
}
if (!CheckWallLeft() && CheckWallRight())
{
if (h < 0)
{
GetComponent().velocity = new Vector2(h * speedHeight, GetComponent().velocity.y);
}
}
if (!CheckGround())
{
GetComponent().velocity = new Vector2(h * speedHeight, GetComponent().velocity.y);
}
if (CheckDown())
{
GetComponent().velocity = new Vector2(h * speedHeight, GetComponent().velocity.y);
}
Подробнее здесь: https://stackoverflow.com/questions/312 ... wall-issue