closeToHero()

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;
  }


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *