InAttackRange()

private bool InAttackRange(int weaponChoice)
    {
        Vector3 characterScale = transform.localScale;
        RaycastHit2D raycastHit;
        bool withinRange = false;

        switch (weaponChoice)
        {
            case 1:
                //throwing

                if (characterScale.x > 0) raycastHit = Physics2D.Raycast(polygonCollider2D.bounds.center, -Vector2.right, polygonCollider2D.bounds.extents.x + optimalThrowRange, heroLayerMask);
                else raycastHit = Physics2D.Raycast(polygonCollider2D.bounds.center, -Vector2.left, polygonCollider2D.bounds.extents.x + optimalThrowRange, heroLayerMask);

                if (Vector2.Distance(transform.position, currentTarget.transform.position) <= optimalThrowRange && raycastHit.collider != null) withinRange = true;
                break;

            case 2:
                //stabbing
                if (characterScale.x > 0) raycastHit = Physics2D.Raycast(polygonCollider2D.bounds.center, -Vector2.right, polygonCollider2D.bounds.extents.x + optimalThrowRange, heroLayerMask);
                else raycastHit = Physics2D.Raycast(polygonCollider2D.bounds.center, -Vector2.left, polygonCollider2D.bounds.extents.x + optimalThrowRange, heroLayerMask);

                if (Vector2.Distance(transform.position, currentTarget.transform.position) <= spearStabRange && raycastHit.collider != null) withinRange = true;
                break;

            case 3:
                //bashing
                if (characterScale.x > 0) raycastHit = Physics2D.Raycast(polygonCollider2D.bounds.center, -Vector2.right, polygonCollider2D.bounds.extents.x + optimalThrowRange, heroLayerMask);
                else raycastHit = Physics2D.Raycast(polygonCollider2D.bounds.center, -Vector2.left, polygonCollider2D.bounds.extents.x + optimalThrowRange, heroLayerMask);

                if (Vector2.Distance(transform.position, currentTarget.transform.position) <= optimalAttackRange && raycastHit.collider != null) withinRange = true;
                break;

            default:
                withinRange = false;
                break;
        }
        return withinRange;
    }


Comments

Leave a Reply

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