Right yeah, I’ve debugged the crap out of this thing. My only other idea is that actionCounter is being set somewhere else every frame, but tbh there is nowhere that it makes sense for that to happen. Just in case, I debugged those functions, and actionCounter always remains at 2 (or whatever is in the inspector) no matter what I do.
Just to provide some more context, I’m copying the full Update() code here to see if that helps at all explain. I’ll also include any other mentions of actionCounter.
void Update()
{
if (!bPhaseIsOn)
{
if (randomizeCounter > 0)
{
isCountingDown = true;
randomizeCounter -= Time.deltaTime;
}
else
{
isCountingDown = false;
RandomizeMovement();
}
if (!shouldRun)
{
if (!isCountingDown)
{
canSetVelocity = true;
randomizeCounter = randomizeMovementCounter;
}
wanderDirection = new Vector3(moveRangeX, moveRangeY, 0);
rotateDirection = theRB.velocity;
moveDirection = wanderDirection;
moveDirection.Normalize();
if (canMove)
{
moveSpeed = moveSpeedWander;
}
}
else
{
rotateDirection = transform.position - PlayerController.instance.transform.position;
moveDirection = transform.position - PlayerController.instance.transform.position;
moveDirection.Normalize();
canSetVelocity = true;
}
float angle = Mathf.Atan2(rotateDirection.y, rotateDirection.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
SetVelocity();
if (theRB.velocity.x < 0 && theRB.velocity.y < 0)
{
transform.localScale = new Vector2(1, -1);
}
if (theRB.velocity.x > 0 && theRB.velocity.y < 0)
{
transform.localScale = new Vector2(1, 1);
}
if (theRB.velocity.x < 0 && theRB.velocity.y > 0)
{
transform.localScale = new Vector2(1, -1);
}
if (theRB.velocity.x > 0 && theRB.velocity.y > 0)
{
transform.localScale = new Vector2(1, 1);
}
}
else
{
if (bPhaseBossFightActive)
{
//print("theRB.velocity.x = " + theRB.velocity.x);
if (theRB.velocity.x > -0.1f)
{
return;
}
else
{
transform.localScale = new Vector3(-1f, 1f, 1f);
}
print("actionCounter = " + actionCounter);
if (actionCounter > 0)
{
Debug.Log("actionCounter is counting down");
actionCounter -= Time.deltaTime;
//Movement
moveDirection = Vector2.zero;
if (actions[currentAction].shouldMove)
{
if (actions[currentAction].shouldChasePlayer)
{
moveDirection = PlayerController.instance.transform.position - transform.position;
moveDirection.Normalize();
}
if (actions[currentAction].shouldMoveToPoints && Vector3.Distance(transform.position, actions[currentAction].pointToMoveTo.position) > .5f)
{
moveDirection = actions[currentAction].pointToMoveTo.position - transform.position;
moveDirection.Normalize();
}
}
theRB.velocity = moveDirection * actions[currentAction].moveSpeed;
//Shooting
if (actions[currentAction].shouldShoot)
{
shotCounter -= Time.deltaTime;
if (shotCounter <= 0)
{
shotCounter = actions[currentAction].timeBetweenShots;
if (//DataManager.instance.difficulty > 0
PlayerPrefs.GetInt("difficulty") > 0)
{
foreach (Transform t in actions[currentAction].shotPoints)
{
Instantiate(actions[currentAction].itemToShoot, t.position, t.rotation);
AudioManager.instance.PlaySFX(enemyShootSound);
}
}
}
}
}
else
{
currentAction++;
if (currentAction >= actions.Length)
{
currentAction = 0;
}
//print("actionCounter = " + actionCounter);
//print("actions.Length = " + actions.Length);
//print("actions[currentAction] = " + actions[currentAction]);
//print("actions[currentAction].actionLength = " + actions[currentAction].actionCounterLength);
actionCounter = actions[currentAction].actionCounterLength;
}
}
}
if (!canMove)
{
moveSpeed = 0;
ResetHeadPosition();
}
else
{
SetVelocity();
}
}
Then this is where I push the enemy to a new sequence, and reset the actionCounter (of course the next sequence never begins, because the actionCounter never counts down in the first place):
private void NextSequence()
{
PlayWakeUpSound();
currentSequence++;
actions = sequences[currentSequence].sequenceActions;
currentAction = 0;
actionCounter = actions[currentAction].actionCounterLength;
anim.SetBool("isWalking", false);
}
This is where the initial actionCounter is set for the first time. Considering it’s an IEnumerator called only once, it won’t ever be running more than once:
public IEnumerator CreateHead()
{
//print("should create head");
anim.SetTrigger("createHead");
anim.SetBool("isIdle", false);
yield return new WaitForSeconds(1f);
bPhaseBossFightActive = true;
//UIController.instance.bossHealthBar.gameObject.SetActive(true);
maxHealth = 1200;
if (PlayerPrefs.GetInt("difficulty") >= 2)
{
newHealth = Mathf.FloorToInt(maxHealth * 2f);
maxHealth = newHealth;
}
currentHealthB = maxHealth;
actions = sequences[currentSequence].sequenceActions;
actionCounter = actions[currentAction].actionCounterLength;
UIController.instance.bossHealthBar.maxValue = maxHealth;
UIController.instance.bossHealthBar.value = currentHealthB;
//CheckAllAnimationStates();
}