// Update is called once per frame void Update() { if (enemyController.freezeEnemy) { // add anything here to happen while frozen i.e. time compensations return; } // Mambu has two states - Closed and Open - and alternates between them while in play // Closed - invincible (set in animation) and moves across the screen for closedTimer time // when closedTimer expires then switches to his Open state and sets two timers // openTimer (how long his shell remains open) and // shootTimer (how long to wait before shooting his 8 bullets) // an alternative to the shootTimer approach could be to define a length to his // Open animation and add another animation event to do the shooting - I took this // approach when I created Screw Driver because he does multiple firings - but Mambu // is a single shot so I just use a timer instead of dragging out the animation // Open - vulnerable to bullets (set in animation) and doesn't move on screen. animation // switches to Open, shootTimer counts down then shoots his bullets when time is up. // shootTimer is half of how long his shell stays open, so being one second open // he fires at half a second in. there is a flag isShooting that is false initially // and once he fires gets set to true so he doesn't keep firing - this ensures a single // occurrence. openTimer counts down while in this state and once expires switches to // his Closed state, sets closedTimer, and resets isShooting back to false. // Note: the invincibility doesn't have to be set via animation events, they could be set during // state changes by calling enemyController.Invincible(bool); however I wanted to show // how to use the animations events for learning purposes. switch (mambuState) { case MambuState.Closed: animator.Play("Mambu_Closed"); rb2d.velocity = new Vector2(((isFacingRight) ? moveSpeed : -moveSpeed), rb2d.velocity.y); closedTimer -= Time.deltaTime; if (closedTimer < 0) { mambuState = MambuState.Open; openTimer = openDelay; shootTimer = shootDelay; } break; case MambuState.Open: animator.Play("Mambu_Open"); rb2d.velocity = new Vector2(0, rb2d.velocity.y); shootTimer -= Time.deltaTime; if (shootTimer < 0 && !isShooting) { ShootBullet(); isShooting = true; } openTimer -= Time.deltaTime; if (openTimer < 0) { mambuState = MambuState.Closed; closedTimer = closedDelay; isShooting = false; } break; } }
public void SetState(MambuState state) { // set mambu state mambuState = state; // not shooting...yet isShooting = false; // set up the state timer we're starting with if (mambuState == MambuState.Closed) { closedTimer = closedDelay; } else if (mambuState == MambuState.Open) { openTimer = openDelay; } }