- Home /
Enemy Attack CoRoutines?
Hello,
Are there any tutorials, or example codes on how to do a shoot em up style boss? See example
This is a bit of a design/code question. i have a very simple boss. I attached 2 missle turrets to the main boss. The boss' health can't be whittled down until the two turret's are destroyed. Once the turrets are destroyed, the enemy starts to create three bullets. After the boss is at half health, the boss will create start another attack phase. (machine gun)
So my dilema: Each phase is controlled via a separate if statement. So it WORKs, but its not exactly fun. Rather than one attack at TIME, I'd rather have a cycle of attacks. Here's a good example on what i'm looking for.
https://www.youtube.com/watch?v=44yzlQTyHQM
Are coroutines better? I tried to create a boss cycle function, but i still don't understand coroutines well enough. (it didn't work) Thanks for any help you can provide.
The coroutines++ video on Unity Gems might help you out with a bit more about using coroutines for multiple step operations.
Here's my code for clarification (current)
function Update(){
if (LevelController.Hardpoint<=0){
//hopefully this only instantiates once here's my code as it stands...
Heading if(LevelController.Turret <=0) { //Start the boss attacks with projectiles?
if (health>75) {
if (Time.time > lastShot + FireFreq)
{ //set up a fire rate
Attack1();
}
}
if (Time.time > lastShot + FireFreq2)
{ //set up a fire rate
Attack2();
}
}
}
}
Answer by Jake.OConnor · Dec 15, 2012 at 06:06 AM
I don't have any links to tutorials or specific code for you (since I don't know what your code looks like), but I can help you out with a basic coroutine to control the boss AI. First of all, start this coroutine once you want the battle to start.
In the coroutine, use while loops with the boss health as the condition and inside do whatever you need to do (call shoot functions, etc) and then yield. Have as many while loops as the boss has attack cycles (you could even make this more abstract to allow you to set multiple boss types easier).
while(m_rBossHealth > 80.0f)
{
vDoBasicAttack();
yield return new WaitForSeconds(m_rBasicAtkTime);
{
while(m_rBossHealth > 50.0f)
{
vDoWoundedAttack();
yield return new WaitForSeconds(m_rWoundedAtkTime);
}
while(m_rBossHealth > 20.0f)
{
vDoSevereAttack();
yield return new WaitForSeconds(m_rSevereAtkTime);
}
while(m_rBossHealth > 0.0f)
{
vDoCriticalAttack();
yield return new WaitForSeconds(m_rCriticalAtkTime);
}
Destroy(this.gameObject);
This sort of coroutine will automatically have your boss do his attacks at the speed that you set and will destroy him once he's dead. Just replace and rework to fit your specific code requirements.
Thanks Jake!
Here's my code, it actually is kind of conditional based. It would be nice if I could also run two attacks at once (ie call 2 functions) Also, how do I declare the code above? Is it Coroutine (insert code)? Thanks
Your answer
Follow this Question
Related Questions
Inconsistent script results/error 1 Answer
Enemy AI kills player once it gets really close? 1 Answer
How to make Enemy AI and Health 1 Answer
how to refer to enemy health 2 Answers
How to issue order to enemies 1 Answer