//射线检测
public void chekGround()
{
//射线检测地面
//1<<8使得射线只检测索引为8的Ground层,防止检测到其他层
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 1, 1 << 8);//1<<8表示开启索引为8的层
Debug.DrawLine(transform.position, transform.position + Vector3.down * 0.5f, Color.red);//绘制射线
if (hit.collider != null)
{
if (hit.collider.gameObject.name == "Ground")
{
//如果射线检测到的是地面
isGround = true;
}
else
{
isGround = false;
}
}
else
{
isGround = false;
}
}
2.碰撞体检测
public void checkGroundAndType()
{//碰撞体检测地面
if (collider.IsTouchingLayers(1 << 8) )
{
isGround = true;
}
else
{
isGround = false;
}
}