Why disable unranged enemy attacks altogether?

Unique Video Reference: 5_BH_UPC

At about 13:20 in the EnemyAI's Attacking() method, a check on the attack range is added so the actual call to Attack() in the enemyType is only run if there’s a range to it, effectively disabling attacks for enemies that don’t use a ranged attack (and might also be too stupid to detect the player from a distance to get into some state of approaching the player until they’re in range to hit).

Right now, this would apply to the slimes that (as of now) have no attack at all.

But if one adds another enemy that does a melee attack (for example that TV guy from the pixel art contest with his “laser sword”), they should still be able to attack as well. (Actually there would still be some attack range for switching to the Attacking state, so a melee fighter wouldn’t just roam randomly until the player bumped into them (well, maybe the slimes would), but that would probably require a more elaborate implementation which might also have a FollowPlayer state, and Attacking would be “in range of weapon so the enemy can actually hit”…)

And let’s not ignore the fact that the range is defaulted to 5, so without setting it to 0 on the slimes they would still try to attack?

I think it would be much better to just guard the Attack() call (so we don’t get errors on enemies that can’t attack like the slimes):

    void Attacking()
    {
        if (canAttack)
        {
            canAttack = false;
            (enemyType as IEnemy)?.Attack();
            StartCoroutine(AttackCooldownRoutine());
        }
    }

just as we did with dealing damage when the “other” actually isn’t the player in the friend/foe detction over here:

        // friend/foe detection
        if ((player && isEnemyProjectile) || (enemyHealth && !isEnemyProjectile))
        {
            // player takes damage
            player?.TakeDamage(1, transform);
            Instantiate(particleOnHitVFXPrefab, transform.position, transform.rotation);
            Destroy(gameObject);
        }
2 Likes

I have noticed that you ask a lot of questions, which is great, but maybe you should take a less advanced tutorial so that you fully grasp everything, just a suggestion.

Do you think I’m so active because I don’t understand the course?

Most of it is because it’s still in early access and I want to point out issues I come across or suggest possible improvements. :wink:

3 Likes

Oh I see, there must be a lot of issues then😉

I might be nitpicking on some of them :laughing:

1 Like

Nice solution, Thanks for sharing :slight_smile:

1 Like

Privacy & Terms