Answers for "how to make enemy killed by bullet unity2D"

C#
0

how to make enemy killed by bullet unity2D

//Put this code on your enemy
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;

  public class EnemyScript : MonoBehaviour
  {
    float health = 10;
    // use OnCollisionEnter2D if you don't have trigger on your bullet
    void OnTriggerEnter2D(Collider2D other)
        {
            if(other.gameObject.CompareTag("Bullet"))
            {
                // You just put script where you have saved your bullet's damage 
                float Damage = other.GetComponent<BulletScript>.Damage;
                health -= Damage;
                if(health <= 0)
                {
                    Destroy(gameObject);
                }
            }
      }
  }
//Put this on your bullet and give your bullet new tag "Bullet"
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;

  public class BulletScript : MonoBehaviour
  {
      public Damage = 5;
  }
// after that, enemy will get destroyed after getting touched at least twice
// by your bullet;
Posted by: Guest on April-30-2022

Code answers related to "how to make enemy killed by bullet unity2D"

C# Answers by Framework

Browse Popular Code Answers by Language