- Home /
The question is answered, right answer was accepted
Interrupting an Enemy Attack [SOLVED]
Hello All!
I have an enemy which works fine. What I want to do is interrupting it's attack if it gets damaged during the attack animation. I use below script which interrupts the attack animation and plays idle animation instead. The problem is that the damage from enemy attack still registers.
I used coroutines to start and stop attacking. And to call StopCoroutine(EnemyAttack);
I used a static boolean InflictDamage.AttackRecieved == true
from another script. Since animation stops which has no bound to the TakingDamage()
coroutine, I assume my boolean InflictDamage.AttackRecieved
working fine. I believe the stop coroutine function either doesn't work or working very slowly that it can't catch damage register part. Any good solutions for this kind of a issue?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpiderAI : MonoBehaviour
{
public GameObject ThePlayer;
public float TargetDistance;
public float AllowedRange = 40;
public GameObject TheSpider;
public float EnemySpeed;
public int AttackTrigger;
public RaycastHit Shot;
public int DealingDamage;
public Animation AttackAnim;
IEnumerator EnemyAttack;
void Update()
{
EnemyAttack = TakingDamage();
transform.LookAt(ThePlayer.transform);
if (InflictDamage.AttackRecieved == true)
{
TheSpider.GetComponent<Animation>().Play("idle");
StopCoroutine(EnemyAttack);
Invoke("notdeal", 0.5f);
}
else
{
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance <= AllowedRange)
{
EnemySpeed = 0.07f;
if (AttackTrigger == 0)
{
TheSpider.GetComponent<Animation>().Play("walk");
transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
}
}
else
{
EnemySpeed = 0f;
TheSpider.GetComponent<Animation>().Play("idle"); // animation name in quotation mark.
}
}
if (AttackTrigger == 1)
{
if (DealingDamage == 0)
{
EnemySpeed = 0f;
TheSpider.GetComponent<Animation>().Play("attack");
StartCoroutine(EnemyAttack);
}
}
}
}
void OnTriggerEnter()
{
AttackTrigger = 1;
}
void OnTriggerExit()
{
AttackTrigger = 0;
}
void notdeal()
{
DealingDamage = 0;
}
IEnumerator TakingDamage()
{
DealingDamage = 2;
yield return new WaitForSeconds(AttackAnim["attack"].length * (1 / AttackAnim["attack"].speed));
if (SpiderEnemy.GlobalSpider != 0)
{
HealthMonitor.HealthValue -= 1;
}
yield return new WaitForSeconds(0.4f);
DealingDamage = 0;
}
}
Answer by shadowpuppet · May 03, 2020 at 02:30 PM
Maybe make another bool "enemyDamaged". When enemy takes damage and goes to his idle animation set enemyDamaged=true. when he starts another attack, reset it to enemyDamaged=false; then as a condition to the player taking damage if(enemyDamaged==false)......perhaps this would be in the coroutine? somewhere, wherever the code is that instructs damage to be assessed
Thank you for your answer. I tried your suggestion and yes it did solve the interruption problem. However it also gave birth to another. With this implementation it appears to me that sometimes the enemy deals 2x and even 3x damage at one attack. TakingDamage()
Coroutine should damage the player once everytime its called. I still believe this is related to coroutine behaviour but I'm just a beginner at scripting so I might be wrong as well.
yeah, I get that a lot when picking up ammo or instantiating ragdolls. 2 or 3 times as many ragdolls or too much ammo and , yes, too much damage being taken. I think it's the way OnTriggerEnter goes sometimes....my way around that is....yes, another bool. make a bool "tookDamage " and that would be tookDamage=false; as soon as he takes damage , tookDamage=true; and then reset it to false in a coroutine so he can take more damage;This way once damage happens for that particular hit it won't happen again until next hit
void takesDamage(){
if (tookDamage ==false){
//damage code here;
tookDamage=true;
StartCoroutine(DelayAfterDamage());//coroutine set tookDamage=false; to ready for next hit
}
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Function delay? 1 Answer
Distribute terrain in zones 3 Answers
Issue involving coroutines and animation 0 Answers
Can't properly duplicate an enemy AI 0 Answers