public void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag != "Stairs" || playerController.getIsClimbing()) { return; } GameObject stairs = collision.gameObject; StairController stairController = stairs.GetComponentInParent <StairController>(); leftEndStep = stairController.leftEndStep; rightEndStep = stairController.rightEndStep; stairDirection = stairController.getStairDirection(); float climb = Input.GetAxisRaw("Climb"); if ((climb == 1 || climb == -1) && playerController.getPlayerStairState() != PlayerController.STAIR_STATE.on_stair) { platformerController.enabled = false; playerController.setIsClimbing(true); //Debug.Log("Control taken from platformer controller"); // move player to base of stairs if grounded if (platformerController.GetPlayerGrounded()) { //Debug.Log("Closest End: " + closestEndStep.name); fraction = 0; StartCoroutine(MovePlayerToStairs(player, stairController)); } else { StartCoroutine(SnapPlayerToStairs(player, stairController)); } } }
float moveYOnStairs(int directionToMove, StairController.STAIR_DIRECTION stairDireciton) { if (stairDireciton == StairController.STAIR_DIRECTION.up) { // if player is moving right on upwards stairs they move up, otherwise they move down return((directionToMove == 1) ? 0.5f : -0.5f); } // if player is moving right on downwards stairs they move down, otherwise they move up return((directionToMove == 1) ? -0.5f : 0.5f); }
/* * CheckBound returns an int representing whether the player is attempting to move off of the bounded stairs. * If the player is at the right end and tries to continue moving right, return 1 * If the player is at the left end and tries to continue moving left, return -1 * If the player is at and end, but not continuing in that direction, return 0 */ int CheckBound(float targetX, int directionToMove, StairController.STAIR_DIRECTION stairDirection) { // player is attempting to move right if (stairDirection == StairController.STAIR_DIRECTION.up) { if (directionToMove == 1) { // player is at the right end if (targetX > stairController.rightEndStep.transform.position.x - 0.5f) { // return 1 to tell move script to move player off the right end return(1); } } else if (directionToMove == -1) { if (targetX < stairController.leftEndStep.transform.position.x) { return(-1); } } } else { if (directionToMove == 1) { // player is at the right end if (targetX > stairController.rightEndStep.transform.position.x - 0.125f) { // return 1 to tell move script to move player off the right end return(1); } } else if (directionToMove == -1) { if (targetX < stairController.leftEndStep.transform.position.x + 0.5f) { return(-1); } } } return(0); }
void OnTriggerStay2D(Collider2D other) { if (other.tag != "Stairs") { return; } stairController = other.GetComponentInParent <StairController>(); if (Input.GetAxis("Climb") != 0 && levelManager.playerController.GetStairState() != PlayerController.STAIR_STATE.on_stair && levelManager.playerController.GetJumpState() == PlayerController.JUMPING.grounded && hasLeftStairTrigger) { stairDirection = stairController.GetStairDirection(); hasLeftStairTrigger = false; player.GetComponent <Rigidbody2D>().gravityScale = 0; levelManager.playerController.SetStairState(PlayerController.STAIR_STATE.on_stair); levelManager.playerController.playerAnimator.SetBool("onStair", true); transitionPercent = 0f; MovePlayerToStairs(stairController, other.gameObject); } }
void MoveOnStairs(int directionToMove, StairController.STAIR_DIRECTION stairDirection) { isMovingOnStairs = true; playerPos = levelManager.playerController.player.transform.position; Vector2 targetPos = Vector2.zero; //target.x is the same regardless of stairDirection targetPos.x = playerPos.x + MoveXOnStairs(directionToMove); //target.y is determined by move direction and stair direction //target.y is the same as stair direction on right and inverted on left targetPos.y = playerPos.y + moveYOnStairs(directionToMove, stairDirection); FlipOnStairs(targetPos.x); if ((!isMovingToStairs || !isMovingOffStairs) && CheckBound(targetPos.x, directionToMove, stairDirection) == 0) { runAfterMoving = RunAfterMovingOnStairs; if (stairController.GetStairDirection() == StairController.STAIR_DIRECTION.up) { levelManager.playerController.playerAnimator.SetInteger("stairDirection", directionToMove); } else { levelManager.playerController.playerAnimator.SetInteger("stairDirection", -directionToMove); } StartCoroutine(MovePlayer(targetPos, climbSpeed)); } else if (CheckBound(targetPos.x, directionToMove, stairDirection) != 0) { MovePlayerOffStairs(directionToMove); } else { levelManager.playerController.playerAnimator.SetInteger("stairDirection", 0); isMovingOnStairs = false; } }