I’m trying to enemy take damage which is set on Shooting script.
Enemy Script:
public class SimpleAI : MonoBehaviour
{
[SerializeField] Transform target;
NavMeshAgent agent;
public GameObject player;
private Shooting shootingScript;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
agent.updateRotation = false;
}
// Update is called once per frame
void Update()
{
agent.SetDestination(target.position);
if(Health == 0)
{
Destroy(gameObject);
}
}
public void PlayerDamage()
{
gameObject player = gameObject.Find("Player");
shootingScript = player.GetComponent<Shooting>();
}
//Health
public int Health = 5;
private void OnTriggerEnter2D(Collider2D other)
{
Health -= player.damage;
}
}
Shooting script:
public class Shooting : MonoBehaviour
{
public Transform firepoint;
public GameObject bulletPrefab;
public float fireRate = 0.5F;
private float nextFire = 1.5F;
public int maxAmmo = 10;
private bool isReloading = false;
private int currentAmmo;
public float bulletForce = 20f;
public float reloadTime = 1f;
public Text ammoUI;
public GameObject reloading;
public GameObject ammoUi;
public int damage = 1;
// Update is called once per frame
void Start()
{
ammoUi.SetActive(true);
}
void Update()
{
ammoUI.text = currentAmmo.ToString();
if (isReloading)
{
return;
}
if(currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if(Input.GetButtonDown("Fire1")&& Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Shoot();
}
}
IEnumerator Reload()
{
isReloading = true;
reloading.SetActive(true);
Debug.Log("Reloading");
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
reloading.SetActive(false);
isReloading = false;
}
void Shoot()
{
currentAmmo--;
GameObject bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firepoint.up * bulletForce, ForceMode2D.Impulse);
}
}