/// <summary> /// Do Euler physics each tick /// </summary> void Update() { if (Time.deltaTime > 0.25f) { return; // quit early, do nothing, if lag spike } anim.SetBool("isGrounded", isGrounded); // communicates to animation controller whether or not the payer isGrounded anim.SetBool("isIdle", isIdle); // communicates to animation controller whether or not the payer isIdle CalcHorizontalMovement(); // Runs HorizontalMovement method CalcVerticalMovement(); // Runs VerticalMovement method // Applies velocity to position transform.position += velocity * Time.deltaTime; aabb.RecalcAABB(); isGrounded = false; // sets isGrounded to false every frame if (playerLocation.position.x < cam.position.x - 14) // If the player is off the left edge of the screen they die { // Had trouble calling this function from HealthSystem so recreated it here Destroy(gameObject); // Kills the player if they end up off the left edge of the screen SoundEffectBoard.PlayDie(); // plays death audio } else if (playerLocation.position.x > cam.position.x + 12.5f) // If the player is touching the right edge of the screen restrict their movement { velocity.x = 0; // Player can no longer move right if touching the right edge of the screen } }
// Health behavior: public void TakeDamage(float amt) { if (cooldownInvulnerability > 0) { return; // still have i-frames, dont take damage } cooldownInvulnerability = .25f; // cooldown till you can take damage if (amt < 0) { amt = 0; // Negative numbers ignored } health -= amt; if (health > 0) { SoundEffectBoard.PlayDamage(); // plays damage audio } if (health <= 0) { Die(); // if health drops to/below zero do Die method SoundEffectBoard.PlayDie(); // plays death audio } }