I’m working on a 2d game and for some reason the delay i’m trying to add from the
private IEnumerator AttackcomboDelay() is not taking effect in my “animation exit boot reset” for loop.
I’ve put the StartCoroutine(AttackcomboDelay()); right before all of the “animator.SetBool(weapon + “Attack” + (1), false);” bools but it’s just turning them all false immediately instead of waiting the 3 seconds like i wanted it to
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCombat : MonoBehaviour
{
public Animator animator;
public Transform attackPointRight;
public Transform attackPointLeft;
public float attackRange = 0.5f;
public LayerMask enemyLayers;
private bool lookingRight = true;
public int attackDamage = 20;
//combo system
public string weaponName;
public int comboLength;
[SerializeField] Weapon currentWeapon;
int comboCounter;
float cooldownTime = 0.1f;
float lastCLicked;
float lastComboEnd;
// Update is called once per frame
void Awake()
{
}
void Start()
{
Transform Visual = transform.Find("Visual");
animator = Visual.GetComponent<Animator>();
}
void Update()
{
if (currentWeapon != null)
{
Attack(currentWeapon.weaponName);
}
float horizontalInput = Input.GetAxis("Horizontal");
if(horizontalInput > 0)
{
lookingRight = true;
}
else if(horizontalInput < 0)
{
lookingRight = false;
}
}
void Attack(string weapon)
{
if(Input.GetButtonDown("Fire1") && Time.time - lastComboEnd > cooldownTime)
{
//New combo Code
comboCounter++;
comboCounter = Mathf.Clamp(comboCounter, 0, currentWeapon.comboLength);
//create attack names
for(int i = 0; i < comboCounter; i++)
{
if(i == 0)
{
if(comboCounter == 1 && animator.GetCurrentAnimatorStateInfo(0).IsTag("Movement"))
{
animator.SetBool("AttackStart", true);
animator.SetBool(weapon + "Attack" + (i + 1), true);
lastCLicked = Time.time;
}
}
else
{
if(comboCounter >= (i + 1) && animator.GetCurrentAnimatorStateInfo(0).IsName(weapon + "Attack" + i))
{
animator.SetBool(weapon + "Attack" + (i + 1), true);
animator.SetBool(weapon + "Attack" + (i), true);
lastCLicked = Time.time;
}
}
}
}
//animation exit bool reset
for(int i=0; i < currentWeapon.comboLength; i++)
{
if(animator.GetCurrentAnimatorStateInfo(0).normalizedTime > .9f && animator.GetCurrentAnimatorStateInfo(0).IsName(weapon + "Attack" + (i + 1)))
{
comboCounter = 0;
lastComboEnd = Time.time;
animator.SetBool(weapon + "Attack" + (i + 1), false);
animator.SetBool("AttackStart", false);
StartCoroutine(AttackcomboDelay());
animator.SetBool(weapon + "Attack" + (1), false);
animator.SetBool(weapon + "Attack" + (2), false);
animator.SetBool(weapon + "Attack" + (3), false);
animator.SetBool(weapon + "Attack" + (4), false);
}
}
}
private IEnumerator AttackcomboDelay()
{
yield return new WaitForSeconds(3);
}
private IEnumerator FullanimationDelay()
{
yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length);
}
void OnDrawGizmosSelected()
{
if(attackPointRight == null)
return;
Gizmos.DrawWireSphere(attackPointRight.position, attackRange);
}
}