Why my enemy inflicts his own health even when it is not attacked by player

The enemy inclits it own health and also the player

Hi Imran,

Do you mean that your enemy is having damage inflicted even though the player isn’t involved?

Assuming so, start with the method which causes the damage to be caused and effectively work backwards, e.g. what is calling that method, perhaps another method which is also called my another method etc. Eventually you’ll find what triggers it to occur, perhaps you have it being called from an OnCollisionEnter method and the enemy is colliding with something for example.

Looking at the screenshot, if those yellow balls are fired by the enemy, it looks like there’s a chance that their own projectiles could be causing them harm.

Hope this helps :slight_smile:

my enemy inflicted damage on itself when he attack the player and the player is in radius only

this is my projectile coding

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Projectile : MonoBehaviour {

    
    public float projectileSpeed; //Note other classes can set

    float damageCaused;

    public void SetDamage(float damage)
    {
        damageCaused = damage;
    }


    void OnCollisionEnter(Collision collision)
    {
        Component damageableComponent = collision.gameObject.GetComponent(typeof(IDamageable));
        if (damageableComponent)
        {
            (damageableComponent as IDamageable).TakeDamage(damageCaused);
        }
        // destroy game object tutorial 70
    }
}
1 Like

Hi Imran,

Thanks for posting your code, its typically always better to copy/paste this and then apply code formatting rather than screenshots. Screenshots are however very useful for errors messages or details from the Unity editor.

What I imagine is happening is that your projectile is spawned and make collision with the enemy which is firing it. Because the enemy is able to be damaged, the TakeDamage method is then called on the enemy.

Take a look at the position from which the projectile is instantiated/spawned, make sure it’s above the enemy, so that when it’s instantiated it doesn’t instantly cause a collision. I think this may even be mentioned in one of these early lectures.

Hope this helps :slight_smile:


See also;

Somehow i found the problem when i tuning the stopping distance on the enemy.
I tune it to 1.5 and the projectile will bounce back to the enemy thats why the enemy is affected when he hit the player.
How do i stop this projectile from bouncing back and make it dissappear and not bouncing then dissappear

Aha, good find.

You have some choices, you could make it a trigger, it would then just pass through the object it collides with, or, you could add some code to destroy the projectile.

The latter would probably make more sense. In your Projectile.cs script, within the OnCollisionEnter method you have a comment at the bottom. Just after that, place the following line of code;

Destroy(GameObject) ;

Now when a projectile collides, it will run your original code and then destroy itself.

Hope this helps :slight_smile:

Privacy & Terms