private bool isOnBranchSide()
{
float lessenHeight = 0.5f;
Vector3 front = new Vector3(polygonCollider2D.bounds.min.x + 1, polygonCollider2D.bounds.center.y, 0);
Vector3 back = new Vector3(polygonCollider2D.bounds.max.x - 1, polygonCollider2D.bounds.center.y, 0);
//start ray //direction //length (1/2 the height of the character) //check against this layer
//RaycastHit2D raycastHit4 = Physics2D.Raycast(polygonCollider2D.bounds.center, -Vector2.up, polygonCollider2D.bounds.extents.y + extraHeight, branchLayerMask);
RaycastHit2D raycastHit5 = Physics2D.Raycast(front, -Vector2.up, polygonCollider2D.bounds.extents.y, branchLayerMask);
RaycastHit2D raycastHit6 = Physics2D.Raycast(back, -Vector2.up, polygonCollider2D.bounds.extents.y, branchLayerMask);
Color rayColor;
if (raycastHit5.collider != null || raycastHit6.collider != null)
{
rayColor = Color.green;
}
else
{
rayColor = Color.yellow;
}
//draw ray on screen when Gizmos enabled
//Debug.DrawRay(polygonCollider2D.bounds.center, -Vector2.up * (polygonCollider2D.bounds.extents.y + extraHeight), rayColor);
Debug.DrawRay(front, -Vector2.up * (polygonCollider2D.bounds.extents.y - lessenHeight), rayColor);
Debug.DrawRay(back, -Vector2.up * (polygonCollider2D.bounds.extents.y - lessenHeight), rayColor);
//return raycastHit4.collider != null;
if (raycastHit5.collider != null)
{
return raycastHit5.collider != null;
}
else if (raycastHit6.collider != null)
{
return raycastHit6.collider != null;
}
else
{
return raycastHit5.collider != null;
}
}
Leave a Reply