- Home /
How can I make my enemy attack me every few seconds?
This is the script that I'm using for the enemy to "attack" my player. What it basically does is that the script looks for whatever collided with my player and checks if it is tagged Enemy, if it is then the player takes damage. The only problem is that the player only takes damage when the enemy enters the trigger. I would like it to be so while the enemy is inside the trigger, it can constantly damage the player every couple of seconds. Thanks for the help and I'll be clearer if needed. public const int maxHealth = 100;
public int currentHealth = maxHealth;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Enemy"))
{
currentHealth -= 10;
if (currentHealth <= 0)
{
currentHealth = 0;
Destroy(gameObject);
}
}
}
public void TakeDamage(int amount)
{
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 0;
Destroy(gameObject);
}
}
}
Answer by SohailBukhari · May 18, 2017 at 11:16 AM
use OnTriggerStay instead of OnTriggerEnter because OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger.
This message is sent to the trigger and the collider that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.
void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("Enemy"))
{
currentHealth -= 10;
if (currentHealth <= 0)
{
currentHealth = 0;
Destroy(gameObject);
}
}
}
OnTriggerStay can be a co-routine, simply use the yield statement in the function.
You can call TakeDamage using Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.
InvokeRepeating("TakeDamage", 2.0f, 0.3f);
So would I just change OnTriggerEnter to OnTriggerStay making sure that one of the colliders has a rigidbody. Also I'm not entirely sure what you mean by OnTriggerStay being a co-routine by using a yield statement. I don't think I was clear, but I am not the best with coding, but I am trying to learn as much as I can. If you could maybe elaborate a bit more on what you mean with some examples, that would be very helpful. But I shall try to do it by myself using what you told me. I will try this to see if it works so thanks.
Just try and if you stuck anywhere then post code and we will solve the problem, if you try yourself then you can learn fast and better.
Answer by dashticle · May 19, 2017 at 02:03 PM
You should read up on coroutines here:
https://docs.unity3d.com/Manual/Coroutines.html
Or search them on the Learn portal. Basically you write a function that returns IEnumerator, and call it with StartCoroutine(yourCoroutine). By using the yield statement, it will get called every x seconds as long as you wish.
The link has som simple examples and a better explanation.
Answer by anilthapliyal8024 · May 22, 2017 at 04:32 PM
As far as I remember Coroutine doesn't work correctly if you write inside OnTriggerEnter().
Your answer
Follow this Question
Related Questions
How to Make Health Decrease Over Time 2 Answers
Stop movement while attacking 1 Answer
Pause Time when certain level loaded? 3 Answers
Digital Clock: Time.time > 60 2 Answers