Home > Mobile >  Enemy shooting itself - Unity C#
Enemy shooting itself - Unity C#

Time:11-06

The problem: When the enemy is alive its bullet scripts explosion effects happens on its body object repeatedly. When I kill the enemy, bullets start shooting from it as it is supposed to be. Why the bullets work only when the enemy is dead? screenshot from bullet exploding on gameobject

Bullet script :

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

public class BulletScriptEnemy : MonoBehaviour
{

    GameObject target;
    public float speed;
    Rigidbody2D bulletRB;
    //
    public GameObject hitEffect;
    public int damage = 40;
    public Rigidbody2D rb;
    //

    
    // Start is called before the first frame update
    void Start()
    {
        bulletRB = GetComponent<Rigidbody2D>();
        target = GameObject.FindGameObjectWithTag("Player");
        Vector2 moveDir = (target.transform.position - transform.position).normalized * speed;
        bulletRB.velocity = new Vector2(moveDir.x, moveDir.y);
        Destroy(this.gameObject, 2);
    }
    
    void OnTriggerEnter2D(Collider2D hitInfo)
   {
GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
Destroy(effect, 2f); //Sekundes 
Destroy(gameObject);
      Player player = hitInfo.GetComponent<Player>();
      if(player != null)
      {
         player.TakeDamage(damage);
      }
      Destroy(gameObject);
   }

    // Update is called once per frame
    void Update()
      {
       
    }

}

Script for the enemy:

public class Enemy : MonoBehaviour
{
    public Animator animator;
    public int maxHealth = 100;
    //public GameObject deathEffect;
    int currentHealth;
    // Start is called before the first frame update
    void Start()
    {
        currentHealth = maxHealth;
    }

    public void TakeDamage(int damage)
    {
        currentHealth -= damage;
        //Play hurt animation
        animator.SetTrigger("Hurt");

        if(currentHealth <= 0)
        {
            Die();
        }
    }
    void Die()
    {   
        Debug.Log("Enemy died!");
        //Instantiate(deathEffect, transform.position, Quaternion.identity);
        //Destroy(gameObject);
        //Die anim
        animator.SetBool("IsDead", true);
        //Disable the enemy
        GetComponent<Collider2D>().enabled = false;
        this.enabled = false;
    }
    }

CodePudding user response:

Is it possible that enemy has a collider too? When the bullet is instantiated its OnTriggerEnter2D is being called because it hits the enemy. Inside the OnTriggerEnter2D you should first check if the gameObject that you collided with is the player. Something like this:

void OnTriggerEnter2D(Collider2D hitInfo)
{
    Player player = hitInfo.GetComponent<Player>();
    if (player == null)
        return;

    GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
    Destroy(effect, 2f); //Sekundes 
    Destroy(gameObject);
    player.TakeDamage(damage);
}

CodePudding user response:

Thanks for the answers. Yes the enemy had collider and changing the code to this solved the issue:

void OnTriggerEnter2D(Collider2D hitInfo)
{
    Player player = hitInfo.GetComponent<Player>();
    if (player == null)
        return;

    GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
    Destroy(effect, 2f); //Sekundes 
    Destroy(gameObject);
    player.TakeDamage(damage);
}
  • Related