- Home /
Question by
JKGameDev · Aug 27, 2017 at 02:00 AM ·
enemy aienemy healthenemy damagehealth-deductionhealth
I am making a FPS and I was wondering how i could make it so when the enemy gets close enough he starts to deal damage.
In my game i want it so that when he gets in a certain distance close to the player he deals damage. I'm not sure if i should use OnTriggerEnter or OnCollisonEnter or how to exactly get this box collider on a different object. Im just not sure how to specify the players collider right now what ever it hits it makes the player take damage. Here's what i have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyDamage : MonoBehaviour {
public float StartHealth = 100f;
public Text HealthNumber;
public Image HealthBar;
void Start () {
}
void Update () {
HealthBar.fillAmount = StartHealth / 100;
HealthNumber.text = StartHealth.ToString();
}
void OnTriggerEnter(Collider other)
{
StartCoroutine(Damage());
}
void OnTriggerExit(Collider other)
{
StartCoroutine(WaitForDamage());
StopCoroutine(Damage());
}
IEnumerator Damage()
{
StartHealth -= 5;
yield return new WaitForSeconds(3);
Update();
}
IEnumerator WaitForDamage()
{
yield return new WaitForSeconds(3);
Update();
}
}
Comment