Reputation: 156
I already know how to call a function only once from Update()
void Update(){
if(x == "blah" && once == true)
{
//call a function here
once = false;
}
}
But how can I do this multiple times?
I have a enemy (boss) and I want to call a method when his health level reaches 0.75, 0.5, 0.25 (but only once each time).
This is my code BTW:
//In the update method :
if (health <= StartHealth * 0.75)
{
Escape();
}
// in the function being called :
else if(health<= StartHealth * 0.75f && health > StartHealth * 0.5f)
{
for(int i = 0; i<2; i++)
{
GameObject x = Instantiate(clone, clonePointz[i].position, Quaternion.identity) as GameObject;
}
}
else if(health <= StartHealth * 0.5f && health > StartHealth * 0.25f)
{
for (int i = 0; i < 4; i++)
{
GameObject x = Instantiate(clone, clonePointz[Random.Range(0, 2)].position, Quaternion.identity) as GameObject;
}
}
else if(health<= 0.25f * StartHealth)
{
for (int i = 0; i < 6; i++)
{
GameObject x = Instantiate(clone, clonePointz[Random.Range(0, 2)].position, Quaternion.identity) as GameObject;
}
}
Upvotes: 1
Views: 4817
Reputation: 216
I don't have Unity at hand right now, but here's a little C# program that should do what you want it to and simulates the engine's Update
calls.
It remembers the health value when Update
was last called and only acts (i.e. spawns some minions) if the boss' health value has passed one of your thresholds between two Update
calls.
void Main()
{
var boss = new BossBehaviour();
boss.Damage(0.1f);
boss.Update();
boss.Damage(0.3f);
boss.Update();
boss.Damage(0.15f);
boss.Update();
boss.Update();
boss.Damage(0.4f);
boss.Update();
}
class BossBehaviour {
float _currentHealth = 1.0f;
float _healthFromLastUpdate = 1.0f;
public void Update(){
Console.WriteLine("Updating. Current health: {0}", _currentHealth);
if (WasThresholdPassed(0.75f)){
SpawnMinions();
}
if (WasThresholdPassed(0.5f)) {
SpawnMinions();
}
if (WasThresholdPassed(0.25f)) {
SpawnMinions();
}
_healthFromLastUpdate = _currentHealth;
}
bool WasThresholdPassed(float threshold) {
if (_healthFromLastUpdate < threshold)
return false;
if (_currentHealth < threshold)
return true;
return false;
}
void SpawnMinions() {
/* ... */
Console.WriteLine("> Spawning minions. Current health: {0}", _currentHealth);
}
public void Damage(float percentDamage) {
_currentHealth -= percentDamage;
}
}
This is its output:
Updating. Current health: 0,9
Updating. Current health: 0,6
> Spawning minions. Current health: 0,6
Updating. Current health: 0,45
> Spawning minions. Current health: 0,45
Updating. Current health: 0,45
Updating. Current health: 0,04999995
> Spawning minions. Current health: 0,04999995
Upvotes: 2
Reputation: 4826
If you want to do it multiple times and know how many times, why not just use a simple counter variable?
//Have counter initialized to some pre-defined start value
void Update()
{
if(x == "blah" && counter > 0)
{
//call a function here
counter--;
}
}
If I'm misunderstanding something, let me know and I'd be happy to try readjusting my answer.
Upvotes: 1