- Home /
Enemy AI Dealing Damage
Im having some trouble with the Enemy AI in my project, I have it programmed so that it can chase me but and it even "deals damage" so to speak. But there are 2 problems im having. 1: My AI only does the damage once and never deals any again, i know the reason is because in the script ive written im only saying it should print out a number of health and the AI doesnt constantly deal damage, but i dont know how to fix it. 2: If i spawn in multiple AI who have the same script they have the same issues as the first one. Please if someone can help me, that would be great. Thank You. P.S: heres the script:
public Transform Player;
public HealthBar healthBar;
int MoveSpeed = 4;
int MaxDist = 3;
int MinDist = 2;
void Start()
{
HealthSystem healthSystem = new HealthSystem(100);
}
void Update()
{
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
Attack();
}
}
}
void Attack()
{
HealthSystem healthSystem = new HealthSystem(100);
healthBar.Setup(healthSystem);
healthSystem.Damage(10);
}
}
Could you rewrite "1: $$anonymous$$y AI only does the damage once and never deals any again, i know the reason is because in the script ive written im only saying it should print out a number of health and the AI doesnt constantly deal damage, but i dont know how to fix it." I don't quite understand what you are trying to say.
Answer by Xavier78 · Feb 25, 2019 at 03:18 AM
So I see a few problems, first every time Attack() is called it is making a completely new HealthSystem. I would do the fallowing.
public abstract class enemy : MonoBehaviour
{
[SerializeField]
private Player _player;
int MoveSpeed = 4;
int MaxDist = 3;
int MinDist = 2;
// Used to setup class from scripting end, any Serialized fields should have an equivalent parameter here.
public virtual void Setup(Player player)
{
_player = player;
}
protected virtual void Update()
{
transform.LookAt(_player.transform.position);
if (Vector3.Distance(transform.position, _player.transform.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, _player.transform.position) <= MaxDist)
{
Attack();
}
}
}
protected virtual void Attack()
{
_player.Damage(10);
}
}
public class Player : MonoBehaviour
{
[SerializeField]
private HealthBar _healthBar;
[SerializeField]
private static HealthSystem _healthSystem;
public void Start()
{
_healthBar.Setup(_healthSystem);
}
// Used to setup class from scripting end, any Serialized fields should have an equivalent parameter here.
public void Setup(HealthBar healthBar, HealthSystem healthSystem)
{
_healthBar = healthBar;
_healthSystem = healthSystem;
}
// Wrapper method.
public void Damage(float dmg)
{
_healthSystem.Damage(dmg);
}
}
public class HealthSystem : MonoBehaviour
{
[SerializeField]
private float _health = 100;
[SerializeField]
private float _damageCoolDownTime = 0.1f;
private bool isBeingDamaged = false;
// Used to setup class from scripting end, any Serialized fields should have an equivalent parameter here.
public void Setup(float health, float damageCoolDownTime)
{
_health = health;
_damageCoolDownTime = damageCoolDownTime;
}
// Called to try and apply damage.
public bool Damage(float dmg)
{
if (isBeingDamaged)
return false;
_health -= dmg;
StartCoroutine(Damaged());
return true;
}
// Used to wait for damageCoolDownTime so enemy can't keep hitting us, and so only one enemy can hit us at once.
private IEnumerator Damaged()
{
isBeingDamaged = true;
yield return new WaitForSeconds(_damageCoolDownTime);
isBeingDamaged = false;
}
}
Note: This will let only one enemy hit the player at a time, if you want many enemys to hit the player, I would add the time delay to the enemy attack itself, which also should be done in the long run so the damage could match any animation that might go along with it.
@Xavier78 i tried your script and i am having a problem Assets\DealDamage.cs(52,24): error CS1061: 'HealthBar' does not contain a definition for 'Setup' and no accessible extension method 'Setup' accepting a first argument of type 'HealthBar' could be found (are you missing a using directive or an assembly reference?) can you tell me how to fix ( i am not using a health bar i am using hearts system is that the problem?) i would be relly thankfull if you help me with that ,@Xavier78 i tried with you code but i am getting one problem Assets\DealDamage.cs(52,24): error CS1061: 'HealthBar' does not contain a definition for 'Setup' and no accessible extension method 'Setup' accepting a first argument of type 'HealthBar' could be found (are you missing a using directive or an assembly reference?) can you please tell me how to fix it?
As you can see in Xavier78 's HealthSystem
, there is a Setup
function. If your health system does not have this function, simply don't call it
@Hellium i did it now i can start my game but when the enemy hits the player nithing happend....?
Your answer
Follow this Question
Related Questions
My rockets don't do damage? 1 Answer
Why Wont My AI Deal Damage To My FPS On Collision 0 Answers
Bootcamp damage 0 Answers
AI Damage if touched 1 Answer
FPS Tutorial AI Damage not Working 1 Answer