- Home /
seperate the cooldown from multiple enemies
Hi all,
I am struggeling on what seems to be a simple problem. I have a classic cooldown function (c#) to delay the attacks of my enemies. This works fine.
But if multiple enemies are present in range, they seem to all sync their cooldown, I'dd like them of course to each have their own cooldown.
Enemies are prefabs, so they have the same scipts attaches to them
my actual simple cooldown attached to each enemy:
private float attackTimerRange;
private float coolDownRange;
void Start(){
attackTimerRange = 0;
coolDownRange = 5.0f;
}
void Update(){
if (attackTimerRange > 0)
{
attackTimerRange -= Time.deltaTime;
}
if (attackTimerRange < 0)
{
attackTimerRange = 0;
}
}
Thanks for any help possible
Hi, each enemy should have their own cool down script, create one universal script for an enemy, and attach it to all enemy's. And I'd suggest writing all cool down related stuff inside separate function, or at least most of it.
if (attackTimerRange > 0)
{
attackTimerRange -= Time.deltaTime;
}
if (attackTimerRange < 0)
{
attackTimerRange = 0;
}
Where do you update attackTimerRange? This says it is pretty much stuck on 0, right?
they do actually. The above is just a part of their attack script, all enemy have this script attached to them. But it seems that the "attackTimerRange" even if it is private, is shared by all of them. This is what I dont understand and would like to prevent
In addition to meat5000's observation of attackTimerRange being stuck at 0, it is also running on each enemy from the point of instantiation ins$$anonymous$$d of when it's shooting.
Answer by meat5000 · Aug 24, 2013 at 03:53 PM
Firstly, change the class to a non-public class.
If all your enemies are present at game start and not instantiated their timers will all start at the same time.
As
attackTimerRange = 0;
and
if (attackTimerRange == 0)
{
attackTimerRange = coolDownRange;
seem to be the only things that modify this, they will always be in sync.
Try something like:
void Start()
{
attackTimerRange = Random.Range(0,11);
}
To add a random element to each Enemy.
Also consider using float instead of int if you want a larger degree of difference
actually, their timer are set back to coolDown (wich is 5f) each time they attack; this works, but it seems that when 1 attacks me, they are all waiting 5seconds before next attack. So its seems the timer is shared.
lol try the suggestion first. I did acknowledge the coolDown in the answer :) Also, is there any particular reason why the class is public? A public class could be the cause of your issue and the scope of the variables is very wide compared to a non-public class in which the variables are not seen from outside.
I'm quite new on Unity and C#, but it seems the class can not be set as private.
changing the Timer time to be a random one would not solve the problem.
Enemy attacks when the attackTimerRAnge is = 0; when they do attack, this timer is set to 5f. When the timer is above 0, it loses 1f per second. thus, after having attacked, the enemy is forced to wait 5 seconds before next attack.
The randomiser at the start serves only to stagger the times across the enemies so they hit 0 at different times. Even if they are all set to 5 this will occur at different times also. Ya get me?
Just take out the word public and see what happens If a variable has scope that extends outside its class or function that same variable is accessible by other classes or functions. As all your enemies share the same variable names AND the class is public this could cause your issue.
indeed! changing the attackTimerRange to a range (float ideally) solves it. I am still unbit unsure why this works, but it does!
thanks a lot
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Syncing times 0 Answers
How do i get an ad cooldowner? 0 Answers
Networking Sync SetActive Not Working 0 Answers