- Home /
StopAllCoroutines not interrupting an AI attack.
Hello, I am creating a Smash Bros like fighting game and the AI is attacking correctly. However, I another character successfully hits them in the startup-lag of the attack, they complete the attack regardless. This gets especially annoying if the other character grabbed them.
Interrupt Snippet
function InterruptAllAttacks () {
Debug.Log(gameObject.name + " Was Interrupted.");
animation.Stop();
GetComponent(SpecialController).InterruptSpecialAttacks();
GetComponent(GrabAndThrow).StopAllCoroutines();
GetComponent(FighterControllerNew).canMove = false;
if(GetComponent(AI)) {
GetComponent(AI).CancelInvoke("CheckAttack");
GetComponent(AI).checking = false;
}
GetComponent(AttackControllerNew).StopAllCoroutines();
}
AI Attack controls
function CheckAttack () {
checking = true;
var pos = transform.TransformPoint(forwardHitPosition);
var pos2 = transform.TransformPoint(upwardHitPosition);
var pos3 = transform.TransformPoint(downwardHitPosition);
var pos4 = transform.TransformPoint(backwardHitPosition);
var enemies : GameObject[] = GameObject.FindGameObjectsWithTag("Hurtbox");
for (var go : GameObject in enemies) {
var enemy = go.GetComponent(Hurtbox);
if (enemy == null)
continue;
if(enemy.num == attacker.playerNo) {
continue;
}
if(enemy.type == "Item") {
continue;
}
var rand = 0;
var num = 0;
var otherNum = 0;
if(rand == 0) {
if (Vector3.Distance(enemy.transform.position, pos) < forwardHitRadius)
{
if(!attacker.busy && controller.OnGround()) {
num = Random.Range(0, 5);
var forwardAttacks : String[] = ["normalA", "sideTilt", "sideSmash", "downTilt", "downSmash"];
attacker.Attack(forwardAttacks[num]);
} else if(!attacker.busy) {
attacker.Attack("forward");
}
}
if (Vector3.Distance(enemy.transform.position, pos2) < upwardHitRadius)
{
if(!attacker.busy && controller.OnGround()) {
otherNum = Random.Range(0, 2);
var upwardAttacks : String[] = ["upTilt", "upSmash"];
attacker.Attack(upwardAttacks[otherNum]);
} else if(!attacker.busy) {
attacker.Attack("upward");
}
}
if (Vector3.Distance(enemy.transform.position, pos3) < downwardHitRadius)
{
if(!attacker.busy && controller.OnGround()) {
} else if(!attacker.busy) {
attacker.Attack("downward");
}
}
if (Vector3.Distance(enemy.transform.position, pos4) < backwardHitRadius)
{
if(!attacker.busy && controller.OnGround()) {
attacker.Attack("downSmash");
} else if(!attacker.busy) {
attacker.Attack("backward");
}
}
}
}
yield WaitForSeconds(waitTime);
checking = false;
}
Any an all help is much appreciated.
What's the name of the Coroutine that you're trying to stop?
Coroutines are run on the GameObject that started it. Are you trying to cancel the coroutines in the same GameObject that started it?
The AI Script is calling the Attack function on the AttackControllerNew. Both these scripts are attached to the AI GameObject. The Interruption is called when the DamageAnd$$anonymous$$nockback attached to the AI GameObject Receives knockback from a different Object(Player, Bomb, Spike Trap, ect). Note that I am using JavaScript.
@hiddenspring81 I am trying to interrupt this function in the AttackControllerNew
function Attack (attackToUse : String) {
busy = true;
var adding = "";
if(!controller.OnGround()) {
adding = "Arial";
}
lastAttackPush = Time.time;
if(adding == "") {
controller.can$$anonymous$$ove = false;
controller.HaltHorizontal();
}
if(GetComponent(AI)) {
GetComponent(AI).waitTime = animation[attackToUse + adding].length;
}
currentAttack = attackToUse;
Debug.Log(currentAttack);
animation.Play(attackToUse + adding);
var initialDelay = DefineWait(attackToUse);
var hitContainer = DefineHitContainer(attackToUse);
var timeWait = DefineDelay(attackToUse);
yield WaitForSeconds(initialDelay);
for(var hit : Hit in hitContainer) {
hit.CreateHit(possibleHurtboxes);
yield WaitForSeconds(timeWait);
}
yield WaitForSeconds(animation[attackToUse + adding].length - timeWait * hitContainer.length - initialDelay);
AfterAttackFunction();
}
This function makes use of a custom class(Hit) to create the hitboxes. In turn, Hit accesses an array of a different class Hitbox.
Answer by ExplodingCookie · Jun 18, 2013 at 08:01 PM
I have solved it, For everyone with a similar problem, call your function with StartCoroutine(functionName) to make it eligible to be halted with StopCoroutine(functionName) and StopAllCoroutines.
Your answer
Follow this Question
Related Questions
Problem with enemy AI 1 Answer
Creating basic enemy animation and attacking 4 Answers
Super Sonic Movement Help 1 Answer
How do I make my AI attack in the FPS tutorial? 0 Answers
Melee range AI 1 Answer