/* -------------------------------------------------------------- */ // basic functions void action() // action function /* ------------------------ Movement ---------------------------- */ { float movement = Input.GetAxis("Horizontal"); // get input axis from player // flip the sprite to facing left or right if (movement > 0 && facingLeft) { flip(); } else if (movement < 0 && !facingLeft) { flip(); } // move player according to the input axis transform.position += new Vector3((movement * speed), 0, 0); /* -------------------------- # ------------------------------ */ /* ------------------------ jump ----------------------------- */ groundChecker.position = new Vector3(transform.position.x, groundChecker.position.y, 0); // update grounchecker's position grounded = Physics2D.OverlapCircle(groundChecker.position, .02f, targetGround); // check if the groundchecker overlap the ground if (grounded) // player currently grounded, player cannot double jump { doubleJump = false; // player cannot do doublejump } if (Input.GetKeyDown(KeyCode.Space) && (grounded || !doubleJump)) { GetComponent <Rigidbody2D> ().AddForce(Vector2.up * jumpForce); // player can jump or double jump if (!doubleJump && !grounded) // player are not on the ground and not double jump yet, player can double jump { doubleJump = true; } } /* ------------------------- # --------------------------- */ /* ----------------------- attack ------------------------------ */ bool attacking = false; if (grounded) { if (playerAttack.getCanAttack()) // check eligibility for the player to attack { if (Input.GetKeyDown(KeyCode.Z)) // get player's input { attacking = true; // player will attack playerEnergy.energyDecrease(20); // player attempt to attack, energy will be decreased by 20 source.PlayOneShot(attackSound); if (playerEnergy.getEnergy() < 20) // if player's energy below the capacity, player cannot attack { playerAttack.setCanAttack(false); } } } else { playerEnergy.isExhausted(playerAttack, energyGUI); // player is exhausted, wait until energy is 100% replinished to attack again } } /* ---------------------- # ----------------------------- */ /* ---------------------- update animator ------------------- */ GetComponent <Animator> ().SetFloat("movement", Mathf.Abs(movement)); GetComponent <Animator> ().SetBool("grounded", grounded); GetComponent <Animator> ().SetBool("attack", attacking); GetComponent <Animator> ().SetFloat("vSpeed", GetComponent <Rigidbody2D> ().velocity.y); /* --------------------------- # --------------------------------- */ }