Help: My timed enemy attack is not attacking at all.
Hey there! Well I've been working on this mechanic for a while now (an embarrassing amount of weeks) and was unable to get it working properly so I decided to see if anyone else experienced this, but I couldn't find any other issues like this, so I came here for help.
So basically, I want my enemy to follow the player, stop once in attack range, wind up an attack (part of the attack animation), then once the animation visibly smashes I want the box collider to activate and trigger if the player is there. The time interval between attacks would be 2 seconds, but if the player escapes the attack I want the enemy to become stuck for double the interval, 4 seconds, until it's free to either move or attack again.
This is the script thats giving me the issues, it's connected to a box collider around the area of the enemy that detects the player, note: this does not damage the player it only detects it to then enable the actual collider that damages the player. These are the variables I'm using:
public GrimaceBehaviour Grim; //Main controller
public Collider2D GrimSmash; //The attack's collider
public FollowDetect follow; //The follow collider/script
public float attkTimer = 2f; //Attack interval
public float attkCD; //Variable to set to interval
public int attkDmg = 2; //The attack's damage
[HideInInspector]
public bool attacking; //Attack anim
[HideInInspector]
public bool stuck; //Stuck in ground anim
[HideInInspector]
public bool inRange = false;
public bool canAttk = false;
These are the methods:
public void Start()
{
attkCD = attkTimer;
Grim = gameObject.GetComponentInParent<GrimaceBehaviour>();
GrimSmash.enabled = false;
follow.enabled = true;
}
void Update()
{
if (attacking == true && canAttk)
{
follow.enabled = false; //stops enemy from moving
}
else
{
follow.enabled = true; //starts movement
}
}
public void OnTriggerStay2D(Collider2D col)
{
if (attkCD > 0)
{
if (col.CompareTag("Player"))
{
inRange = true;
follow.enabled = false;
attacking = false;
attkCD -= Time.deltaTime; //countdown to attack
}
}
else
{
canAttk = true;
Grim.anim.SetBool("Attack", true);
Pause();
GrimSmash.enabled = true;
attkCD = attkTimer; //resets countdown
}
}
public void OnTriggerExit2D(Collider2D col)
{
if (col.CompareTag("Player"))
{
inRange = false;
follow.enabled = true;
}
}
IEnumerator Pause()
{
yield return new WaitForSecondsRealtime(2f);
}
}
Any insights you can give are greatly appreciated. I know my code can be hard to follow, so if any further clarification is needed I will gladly give it. Thank you very much if you try and assist me, I can admit I dearly need it.
Your answer
Follow this Question
Related Questions
Help 2D attack 1 Answer
Changing gravity scale through collisions 0 Answers
Timer-Based Attack System 0 Answers
Player get's stuck on Tilemap Collider 2D 0 Answers
Charecter keeps playing walking animation after punching 0 Answers