private void GroundState() { if (!_groundSensor.State() && _rigidbody2D.velocity.y != 0) { ChangeState(PlayerState.InAir); return; } Vector2 currentSpeed = new Vector2(0, _rigidbody2D.velocity.y); if (_attackTimer < attackDuration || _blockTimer < blockDuration) { } else if (_attackTimer > attackCooldown && Game.Input.GetAttackInput()) { _attackTimer = 0; _attackVariant = Random.Range(1, 4); _animator.SetTrigger($"Attack{_attackVariant}"); } else if (_blockTimer > blockCooldown && Game.Input.GetBlockInput()) { _blockTimer = 0; _animator.SetTrigger("Block"); } else if (Game.Input.GetRollInput()) { ChangeState(PlayerState.InRolling); currentSpeed = new Vector2(_facingDirection * rollForce, _rigidbody2D.velocity.y); } else if (Game.Input.GetJumpInput()) { ChangeState(PlayerState.InAir); _animator.SetTrigger("Jump"); currentSpeed = new Vector2(_rigidbody2D.velocity.x, jumpForce); _groundSensor.Disable(0.2f); } else { float move = Game.Input.GetHorizontalInput(); if (move != 0) { RotateSprite(move); } currentSpeed = new Vector2(move * speed, _rigidbody2D.velocity.y); } _animator.SetInteger( "AnimState", Mathf.Abs(currentSpeed.x) > Mathf.Epsilon ? 1 : 0); _rigidbody2D.velocity = currentSpeed; }
// Update is called once per frame void Update() { // Increase timer that controls attack combo m_timeSinceAttack += Time.deltaTime; //Check if character just landed on the ground if (!m_grounded && m_groundSensor.State()) { m_grounded = true; m_animator.SetBool("Grounded", m_grounded); } //Check if character just started falling if (m_grounded && !m_groundSensor.State()) { m_grounded = false; m_animator.SetBool("Grounded", m_grounded); } // -- Handle input and movement -- float inputX = Input.GetAxis("Horizontal"); // Swap direction of sprite depending on walk direction if (inputX > 0) { GetComponent <SpriteRenderer>().flipX = false; m_facingDirection = 1; } else if (inputX < 0) { GetComponent <SpriteRenderer>().flipX = true; m_facingDirection = -1; } // Move if (!m_rolling) { m_body2d.velocity = new Vector2(inputX * m_speed, m_body2d.velocity.y); } //Set AirSpeed in animator m_animator.SetFloat("AirSpeedY", m_body2d.velocity.y); // -- Handle Animations -- //Wall Slide m_animator.SetBool("WallSlide", (m_wallSensorR1.State() && m_wallSensorR2.State()) || (m_wallSensorL1.State() && m_wallSensorL2.State())); //Death if (Input.GetKeyDown("e")) { m_animator.SetBool("noBlood", m_noBlood); m_animator.SetTrigger("Death"); } //Hurt else if (Input.GetKeyDown("q")) { m_animator.SetTrigger("Hurt"); } //Attack else if (Input.GetMouseButtonDown(0) && m_timeSinceAttack > 0.25f) { m_currentAttack++; // Loop back to one after third attack if (m_currentAttack > 3) { m_currentAttack = 1; } // Reset Attack combo if time since last attack is too large if (m_timeSinceAttack > 1.0f) { m_currentAttack = 1; } // Call one of three attack animations "Attack1", "Attack2", "Attack3" m_animator.SetTrigger("Attack" + m_currentAttack); // Reset timer m_timeSinceAttack = 0.0f; } // Block else if (Input.GetMouseButtonDown(1)) { m_animator.SetTrigger("Block"); m_animator.SetBool("IdleBlock", true); } else if (Input.GetMouseButtonUp(1)) { m_animator.SetBool("IdleBlock", false); } // Roll else if (Input.GetKeyDown("left shift") && !m_rolling) { m_rolling = true; m_animator.SetTrigger("Roll"); m_body2d.velocity = new Vector2(m_facingDirection * m_rollForce, m_body2d.velocity.y); } //Jump else if (Input.GetKeyDown("space") && m_grounded) { m_animator.SetTrigger("Jump"); m_grounded = false; m_animator.SetBool("Grounded", m_grounded); m_body2d.velocity = new Vector2(m_body2d.velocity.x, m_jumpForce); m_groundSensor.Disable(0.2f); } //Run else if (Mathf.Abs(inputX) > Mathf.Epsilon) { // Reset timer m_delayToIdle = 0.05f; m_animator.SetInteger("AnimState", 1); } //Idle else { // Prevents flickering transitions to idle m_delayToIdle -= Time.deltaTime; if (m_delayToIdle < 0) { m_animator.SetInteger("AnimState", 0); } } }
// Update is called once per frame void Update() { //user hits esc key to bring up pause menu if (Input.GetKeyDown("escape")) { //pause/freeze any animated things Time.timeScale = 0f; //activate pause menu pauseMenu.SetActive(true); //hide pause button as already paused pauseButton.SetActive(false); } // Increase timer that controls attack combo m_timeSinceAttack += Time.deltaTime; //Check if character just landed on the ground if (!m_grounded && m_groundSensor.State()) { m_grounded = true; m_animator.SetBool("Grounded", m_grounded); ManagingAudio.PlaySound("Landing"); } //Check if character just started falling if (m_grounded && !m_groundSensor.State()) { m_grounded = false; m_animator.SetBool("Grounded", m_grounded); } // -- Handle input and movement -- float inputX = Input.GetAxis("Horizontal"); // Swap direction of sprite depending on walk direction if (inputX > 0) //moving right { GetComponent <SpriteRenderer>().flipX = false; m_facingDirection = 1; } else if (inputX < 0) //moving left { GetComponent <SpriteRenderer>().flipX = true; m_facingDirection = -1; } // Move if (!m_rolling) { //translate rigidbody m_body2d.velocity = new Vector2(inputX * m_speed, m_body2d.velocity.y); //rigidbody is moving if (m_body2d.velocity.x != 0) { isMoving = true; } //not moving else { isMoving = false; } //only play moving sound when on ground, moving and game isnt paused if (isMoving && m_grounded && Time.timeScale == 1) { //check sound already playing if (!movementSrc.isPlaying) { movementSrc.PlayScheduled(2.0f); } } else //stop sound { movementSrc.Stop(); } } //facing right if (m_facingDirection == 1) { AttackPtL.SetActive(false); AttackPtR.SetActive(true); FirePointL.SetActive(false); FirePointR.SetActive(true); } else //moving left { AttackPtL.SetActive(true); AttackPtR.SetActive(false); FirePointL.SetActive(true); FirePointR.SetActive(false); } //Set AirSpeed in animator m_animator.SetFloat("AirSpeedY", m_body2d.velocity.y); //Attack //*****was 1f to slow attacks if (Input.GetKeyDown("w") && m_timeSinceAttack > 0.25f) { ManagingAudio.PlaySound("Melee"); m_currentAttack++; // Loop back to one after third attack if (m_currentAttack > 3) { m_currentAttack = 1; } // Reset Attack combo if time since last attack is too large if (m_timeSinceAttack > 1.0f) { m_currentAttack = 1; } // Call one of three attack animations "Attack1", "Attack2", "Attack3" m_animator.SetTrigger("Attack" + m_currentAttack); // Reset timer m_timeSinceAttack = 0.0f; //attack enemy Attack(); } if (Input.GetButtonDown("Fire1")) { if (projectile == null) { Debug.Log("projectile null"); } if (m_facingDirection == 1) { projectile.SendMessage("FacingRight", true); Debug.Log("Trying to send msg"); } else { projectile.SendMessage("FacingRight", false); } } // Block else if (Input.GetKeyDown("e")) { isBlocking = true; m_animator.SetTrigger("Block"); m_animator.SetBool("IdleBlock", true); } //stop blocking else if (Input.GetKeyUp("e")) { isBlocking = false; m_animator.SetBool("IdleBlock", false); } //Jump else if ((Input.GetKeyDown("space") && m_grounded) || (Input.GetKeyDown("up") && m_grounded)) { ManagingAudio.PlaySound("Jump"); m_animator.SetTrigger("Jump"); m_grounded = false; m_animator.SetBool("Grounded", m_grounded); m_body2d.velocity = new Vector2(m_body2d.velocity.x, m_jumpForce); m_groundSensor.Disable(0.2f); } //Run else if (Mathf.Abs(inputX) > Mathf.Epsilon) { // Reset timer m_delayToIdle = 0.05f; m_animator.SetInteger("AnimState", 1); } //Idle else { // Prevents flickering transitions to idle if (currentHealth > 0) { m_delayToIdle -= Time.deltaTime; if (m_delayToIdle < 0) { m_animator.SetInteger("AnimState", 0); } } } // Called in slide animation. void AE_SlideDust() { Vector3 spawnPosition; if (m_facingDirection == 1) { spawnPosition = m_wallSensorR2.transform.position; } else { spawnPosition = m_wallSensorL2.transform.position; } if (m_slideDust != null) { // Set correct arrow spawn position GameObject dust = Instantiate(m_slideDust, spawnPosition, gameObject.transform.localRotation) as GameObject; // Turn arrow in correct direction dust.transform.localScale = new Vector3(m_facingDirection, 1, 1); } } }
// Update is called once per frame void Update() { // Increase timer that controls attack combo m_timeSinceAttack += Time.deltaTime; //Check if character just landed on the ground if (!m_grounded && m_groundSensor.State()) { m_grounded = true; m_animator.SetBool("Grounded", m_grounded); } //Check if character just started falling if (m_grounded && !m_groundSensor.State()) { m_grounded = false; m_animator.SetBool("Grounded", m_grounded); } // -- Handle input and movement -- float inputX = Input.GetAxis("Horizontal"); // Swap direction of sprite depending on walk direction if (inputX > 0) { GetComponent <SpriteRenderer>().flipX = false; m_facingDirection = 1; } else if (inputX < 0) { GetComponent <SpriteRenderer>().flipX = true; m_facingDirection = -1; } // Move if (!m_rolling) { m_body2d.velocity = new Vector2(inputX * m_speed, m_body2d.velocity.y); } //Set AirSpeed in animator m_animator.SetFloat("AirSpeedY", m_body2d.velocity.y); // -- Handle Animations -- //Wall Slide m_animator.SetBool("WallSlide", (m_wallSensorR1.State() && m_wallSensorR2.State()) || (m_wallSensorL1.State() && m_wallSensorL2.State())); //Death if (Input.GetKeyDown("e")) { m_animator.SetBool("noBlood", m_noBlood); m_animator.SetTrigger("Death"); } //Hurt else if (Input.GetKeyDown("q")) { m_animator.SetTrigger("Hurt"); } //Attack else if (Input.GetMouseButtonDown(0) && m_timeSinceAttack > 0.25f) { m_currentAttack++; Collider2D[] collider2Ds = Physics2D.OverlapBoxAll(pos.position, boxSize, 0); foreach (Collider2D collider in collider2Ds) { /*if(m_facingDirection == 1) * { * pos.Translate(new Vector3((float)0.77, (float)0)); * } * else if(m_facingDirection == -1) * { * pos.Translate(new Vector3((float)-0.77, (float)0)); * }*/ if (collider.tag == "Enemy") { Debug.Log(collider.tag); collider.GetComponent <Monster>().TakeDamage(1); break; } else if (collider.tag == "BossEnemy") { collider.GetComponent <Enemy>().TakeDamage(1); break; } } // Loop back to one after third attack if (m_currentAttack > 3) { m_currentAttack = 1; } // Reset Attack combo if time since last attack is too large if (m_timeSinceAttack > 1.0f) { m_currentAttack = 1; } // Call one of three attack animations "Attack1", "Attack2", "Attack3" m_animator.SetTrigger("Attack" + m_currentAttack); PlaySound("ATTACK"); // Reset timer m_timeSinceAttack = 0.0f; } // Block else if (Input.GetMouseButtonDown(1)) { m_animator.SetTrigger("Block"); m_animator.SetBool("IdleBlock", true); } else if (Input.GetMouseButtonUp(1)) { m_animator.SetBool("IdleBlock", false); } // Roll else if (Input.GetKeyDown("left shift") && !m_rolling) { m_rolling = true; m_animator.SetTrigger("Roll"); m_body2d.velocity = new Vector2(m_facingDirection * m_rollForce, m_body2d.velocity.y); gameObject.layer = 11; Invoke("OffDamaged", 1.5f); } //Jump else if (Input.GetKeyDown("space") && m_grounded) { m_animator.SetTrigger("Jump"); m_grounded = false; m_animator.SetBool("Grounded", m_grounded); m_body2d.velocity = new Vector2(m_body2d.velocity.x, m_jumpForce); m_groundSensor.Disable(0.2f); } //UnderJump else if (Input.GetKeyDown("s") && m_grounded) { gameObject.layer = 15; Invoke("OffDamaged", 1.0f); } //Run else if (Mathf.Abs(inputX) > Mathf.Epsilon) { // Reset timer m_delayToIdle = 0.05f; m_animator.SetInteger("AnimState", 1); } //Idle else { // Prevents flickering transitions to idle m_delayToIdle -= Time.deltaTime; if (m_delayToIdle < 0) { m_animator.SetInteger("AnimState", 0); } } }
// Update is called once per frame void Update() { // Increase timer that controls attack combo m_timeSinceAttack += Time.deltaTime; //Check if character just landed on the ground if (!m_grounded && m_groundSensor.State()) { m_grounded = true; m_animator.SetBool("Grounded", m_grounded); } //Check if character just started falling if (m_grounded && !m_groundSensor.State()) { m_grounded = false; m_animator.SetBool("Grounded", m_grounded); } // -- Handle input and movement -- float inputX = Input.GetAxis("Horizontal"); // Swap direction of sprite depending on walk direction if (inputX > 0) { if (transform.localScale.x < 0) { transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z); } m_facingDirection = 1; } else if (inputX < 0) { if (transform.localScale.x > 0) { transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z); } m_facingDirection = -1; } // Move if (!m_rolling) { m_body2d.velocity = new Vector2(inputX * m_speed, m_body2d.velocity.y); } //Set AirSpeed in animator m_animator.SetFloat("AirSpeedY", m_body2d.velocity.y); // -- Handle Animations -- //Wall Slide m_animator.SetBool("WallSlide", (m_wallSensorR1.State() && m_wallSensorR2.State()) || (m_wallSensorL1.State() && m_wallSensorL2.State())); //Attack if (Input.GetMouseButtonDown(0) && m_timeSinceAttack > 0.75f) { m_currentAttack++; // Loop back to one after third attack if (m_currentAttack > 3) { m_currentAttack = 1; } // Reset Attack combo if time since last attack is too large if (m_timeSinceAttack > 1.0f) { m_currentAttack = 1; } // Call one of three attack animations "Attack1", "Attack2", "Attack3" m_animator.SetTrigger("Attack" + m_currentAttack); FindObjectOfType <AudioManager>().Play("sword-attack"); // detects if an enemy was hit Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers); foreach (Collider2D enemy in hitEnemies) { enemy.GetComponent <Bandit>().TakeDamage(99); } // Reset timer m_timeSinceAttack = 0.0f; } // Block else if (Input.GetMouseButtonDown(1)) { m_animator.SetTrigger("Block"); m_animator.SetBool("IdleBlock", true); block = true; } else if (Input.GetMouseButtonUp(1)) { m_animator.SetBool("IdleBlock", false); block = false; } // Roll else if (Input.GetKeyDown("left shift") && !m_rolling) { m_rolling = true; m_animator.SetTrigger("Roll"); m_body2d.velocity = new Vector2(m_facingDirection * m_rollForce, m_body2d.velocity.y); } //Jump else if (Input.GetKeyDown("w") && m_grounded) { m_animator.SetTrigger("Jump"); m_grounded = false; m_animator.SetBool("Grounded", m_grounded); m_body2d.velocity = new Vector2(m_body2d.velocity.x, m_jumpForce); m_groundSensor.Disable(0.2f); FindObjectOfType <AudioManager>().Play("jump"); } //Run else if (Mathf.Abs(inputX) > Mathf.Epsilon) { // Reset timer m_delayToIdle = 0.05f; m_animator.SetInteger("AnimState", 1); } //Idle else { // Prevents flickering transitions to idle m_delayToIdle -= Time.deltaTime; if (m_delayToIdle < 0) { m_animator.SetInteger("AnimState", 0); } } }