void FixedUpdate() { if (isDead) { return; } if (isJumping && grounded && GetComponent <Rigidbody2D>().velocity.y <= 0) { isJumping = false; } float h = Input.GetAxis("Horizontal"); if (climbingLadder != null) { float v = Input.GetAxis("Vertical"); float dir = v == 0 ? 0 : Mathf.Sign(v); Collider2D ladder = FindLadderInDirection((int)dir, spriteHeight * 0.2f); if (ladder != climbingLadder) { if (grounded) { climbingLadder = null; GetComponent <Rigidbody2D>().isKinematic = false; } else { GetComponent <Rigidbody2D>().velocity = new Vector2(0, 0); } } else { GetComponent <Rigidbody2D>().velocity = new Vector2(0, dir * maxSpeed * 0.5f); } // Play walking sound if (Mathf.Abs(GetComponent <Rigidbody2D>().velocity.y) > 0.01) { soundEffectPlayer.PlayWalkEffect(true); } else { soundEffectPlayer.PlayWalkEffect(false); } } else { float dir = h == 0 ? 0 : Mathf.Sign(h); GetComponent <Rigidbody2D>().velocity = new Vector2(dir * maxSpeed, GetComponent <Rigidbody2D>().velocity.y); if (h > 0 && !facingRight) { Flip(); } else if (h < 0 && facingRight) { Flip(); } if (shouldJump) { soundEffectPlayer.PlayJumpEffect(); // Add a vertical force to the player. GetComponent <Rigidbody2D>().AddForce(new Vector2(0f, jumpForce)); // Make sure the player can't jump again until the jump conditions from Update are satisfied. shouldJump = false; isJumping = true; } // Play walking sound if (!isJumping && Mathf.Abs(GetComponent <Rigidbody2D>().velocity.x) > 0.01f) { soundEffectPlayer.PlayWalkEffect(true); } else { soundEffectPlayer.PlayWalkEffect(false); } } // Update the animation state if (anim != null) { anim.SetFloat("Speed", Mathf.Abs(h)); } if (anim != null) { anim.SetBool("Jumping", isJumping); } if (anim != null) { anim.SetBool("Climbing", climbingLadder != null); } }
void Update() { float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); bool pressedJumpButton = Input.GetButtonDown("Jump"); bool isOnGround = Physics2D.Raycast(bottomTransform.position, new Vector2(0, -1), 0.05f, 1 << LayerMask.NameToLayer("Ground")); //-------- Idle/Walking --------// if (currentState == State.Idle || currentState == State.Walking) { float direction = horizontalInput == 0 ? 0 : Mathf.Sign(horizontalInput); GetComponent <Rigidbody2D>().velocity = new Vector2(direction * maxSpeed, GetComponent <Rigidbody2D>().velocity.y); climbingLadder = FindLadderInDirection((int)Mathf.Sign(verticalInput), 0.05f); if (horizontalInput > 0 && !facingRight) { Flip(); } else if (horizontalInput < 0 && facingRight) { Flip(); } // Play walking sound if (direction != 0) { soundEffectPlayer.PlayWalkEffect(true); } else { soundEffectPlayer.PlayWalkEffect(false); } // Check if we should switch state if (pressedJumpButton) { SwitchToState(State.Jumping); } else if (verticalInput > 0 && climbingLadder != null && (direction == 0 || bottomTransform.position.y < climbingLadder.transform.position.y)) { SwitchToState(State.Climbing); } else if (verticalInput < 0 && climbingLadder != null && (direction == 0 || bottomTransform.position.y > climbingLadder.transform.position.y)) { SwitchToState(State.Climbing); } else if (direction != 0 && (climbingLadder == null || (climbingLadder != null && bottomTransform.position.y > climbingLadder.transform.position.y))) { SwitchToState(State.Walking); } else if (direction == 0) { SwitchToState(State.Idle); } } //-------- Jumping --------// else if (currentState == State.Jumping) { if (isOnGround && !shouldJump && timeSinceJumpStarted > 0.1f) { SwitchToState(State.Idle); } } //-------- Climbing --------// else if (currentState == State.Climbing) { float direction = verticalInput == 0 ? 0 : Mathf.Sign(verticalInput); Collider2D ladder = FindLadderInDirection((int)direction, maxSpeed * 0.5f); if (ladder == climbingLadder) { GetComponent <Rigidbody2D>().velocity = new Vector2(0, direction * maxSpeed * 0.5f); } else { GetComponent <Rigidbody2D>().velocity = Vector2.zero; } // Play walking sound if (Mathf.Abs(GetComponent <Rigidbody2D>().velocity.y) > 0.0f) { soundEffectPlayer.PlayWalkEffect(true); } else { soundEffectPlayer.PlayWalkEffect(false); } // Check if we should switch state if (Mathf.Abs(horizontalInput) > 0.0f && isOnGround && GetComponent <Rigidbody2D>().velocity.y == 0) { SwitchToState(State.Walking); } } //-------- Dead --------// else if (currentState == State.Dead) { // Do nothing... } // Update the animation state if (anim != null) { anim.SetBool("Idle", currentState == State.Idle); anim.SetBool("Walking", currentState == State.Walking); anim.SetBool("Jumping", currentState == State.Jumping); anim.SetBool("Climbing", currentState == State.Climbing); } }