admin管理员组

文章数量:1122792

I have been suffering from the same problem for several days. I want to add damage to the attack motion, but I can't solve this problem. Please lend me your wisdom.

public class BaseWeapon : MonoBehaviour
{
    protected WeaponSpawnerStats stats;

    protected void attactEnemy(Collider2D collider2d, float attack)
    {
        
        if(!collider2d.gameObject.TryGetComponent<EnemyController>(out var enemy)) return;
        float damage = enemy.Damage(attack);
        spawner.TotalDamage += damage;
        
        if (0 > stats.HP) return;
        stats.HP--;
        if (0 > stats.HP) Destroy(gameObject);

    protected void attackEnemy(Collider2D collider2d)
    {
        attackEnemy(collider2d, stats.Attack);
        /*
        BaseWeapon.cs(59,3): error CS1501: 
        No overload for method 'attackEnemy' takes 2 arguments
        */
 
   }
}
protected void attackEnemy(Collider2D collider2d)
    {
    attackEnemy(collider2d);
    }

It works, but the attack motion will not contain any damage

I have been suffering from the same problem for several days. I want to add damage to the attack motion, but I can't solve this problem. Please lend me your wisdom.

public class BaseWeapon : MonoBehaviour
{
    protected WeaponSpawnerStats stats;

    protected void attactEnemy(Collider2D collider2d, float attack)
    {
        
        if(!collider2d.gameObject.TryGetComponent<EnemyController>(out var enemy)) return;
        float damage = enemy.Damage(attack);
        spawner.TotalDamage += damage;
        
        if (0 > stats.HP) return;
        stats.HP--;
        if (0 > stats.HP) Destroy(gameObject);

    protected void attackEnemy(Collider2D collider2d)
    {
        attackEnemy(collider2d, stats.Attack);
        /*
        BaseWeapon.cs(59,3): error CS1501: 
        No overload for method 'attackEnemy' takes 2 arguments
        */
 
   }
}
protected void attackEnemy(Collider2D collider2d)
    {
    attackEnemy(collider2d);
    }

It works, but the attack motion will not contain any damage

Share Improve this question edited Nov 25, 2024 at 5:02 derHugo 90.2k9 gold badges90 silver badges134 bronze badges asked Nov 21, 2024 at 22:50 noanoa 232 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The method you are trying to call has a typo: it's called attactEnemy (t instead of k). Updating it to attackEnemy should fix the problem.

Your updated code attackEnemy(collider2d); doesn't cause a compilation error because you're calling the same method recursively. It doesn't do anything though (besides wasting resources), as the method just calls itself in a loop.

本文标签: cHow do I include damage in character motionStack Overflow