private bool closeToHero()
{
RaycastHit2D raycastHit;
Vector3 characterScale = transform.localScale;
if(characterScale.x > 0)
{
//start ray //direction //length //check against this layer
raycastHit = Physics2D.Raycast(polygonCollider2D.bounds.center, -Vector2.right, polygonCollider2D.bounds.extents.x + stickWidth, heroLayerMask);
}else{
raycastHit = Physics2D.Raycast(polygonCollider2D.bounds.center, -Vector2.left, polygonCollider2D.bounds.extents.x + stickWidth, heroLayerMask);
}
Color rayColor;
if(raycastHit.collider != null){
rayColor = Color.yellow;
}else{
rayColor = Color.red;
}
//draw ray on screen when Gizmos enabled
if(characterScale.x > 0){
Debug.DrawRay(polygonCollider2D.bounds.center, -Vector2.right * (polygonCollider2D.bounds.extents.x + stickWidth), rayColor);
}else{
Debug.DrawRay(polygonCollider2D.bounds.center, -Vector2.left * (polygonCollider2D.bounds.extents.x + stickWidth), rayColor);
}
return raycastHit.collider != null;
}
Leave a Reply