Unity Platform 04 Wall Jump & Hop === 2020.08.08 ###### tags: `platform` ## List 1. [Side Wall Check](#SWC) 2. [Side Wall Sliding](#SWS) 3. [Move in Air](#MA) 4. [Cut Jump Height](#CJH) 5. [Wall Jump & Hop](#WJH) ## Content ### Side Wall Check<a id="SWC"/> Like grounding, use obj for check side wall.<br>Linear check using method `Physics2D.RayCast()`. ```csharp= isTouchingWall = Physics2D.Raycast(wallCheck.position, transform.right, wallCheckDistance, WhatIsGround); ``` ### Side Wall Sliding<a id="SWS"/> If player attaching wall, slide with wall line.<br>Check Wall Slide state in update.<br>Move control(velocity control) in FixedUpdate. ```csharp= // In Update() private void CheckIfWallSliding() { if (isTouchingWall && !isGrounded && rb2D.velocity.y < 0) isWallSliding = true; else isWallSliding = false; } // In FixedUpdate() privte coid applyMovement() { ~~~ if (isWallSliding) { if (rb2D.velocity.y < -wallSlidingSpeed) rb2D.velocity = new Vector2(rb2D.velocity.x, -wallSlidingSpeed); } ~~~ } ``` And `Flip()` must be changed.<br>Now, not correct work when direction is other way of wall. ```csharp= protected void Flip() { if (!isWallSliding) // This condition for right position for wallSliding. { isFacingRight = !isFacingRight; transform.Rotate(0.0f, 100.0f, 0.0f); } } ``` ### Move in Air<a id="MA"/> Player in air can move a little bit.<br>Conditions are below. ```csharp= else if (!isGrounded && !isWallSliding && moveDirection != 0) { Vector2 forceToAdd = new Vector2(movementForceInAir * moveDirection, 0); rb2D.AddForce(forceToAdd); if (Mathf.Abs(rb2D.velocity.x) > velocity) rb2D.velocity = new Vector2(velocity * moveDirection, rb2D.velocity.y); } ``` Now, move little in air, but don't stop when you tab off button.<br>So write below for velocity slow down. ```csharp= else if (!isGrounded && !isWallSliding && moveDirection == 0) { rb2D.velocity = new Vector2(rb2D.velocity.x * airDragMultiplier, rb2D.velocity.y); } ``` ### Cut Jump Height<a id="CJH"/> You can push jump button long or short.<br>Jump height is different when this. ```csharp= if (Input.GetButtonUp("Jump")) rb2D.velocity = new Vector2(rb2D.velocity.x, rb2D.velocity.y * jumpHighierMultiplier); ``` ### Wall Jump & Hop<a id="WJH"/> Hop is off to wall, wall_jump is jump at wall_slide.<br>This 3 Step. 1. Hop and WallJump Force Setting / Direction ```csharp= public float wallHopForce; public float wallJumpForce; public Vector2 wallHopDirection; public Vector2 wallJumpDirection; ``` 2. Jump setting when wall_slide ```csharp= protected void Jump() { if (canJump && !isWallSliding) // !isWallSliding is wall jump { rb2D.velocity = new Vector2(rb2D.velocity.x, jumpForce); amountOfJumpsLeft--; } // Below is for Hopping Wall(Just down ward(not jump)) else if (isWallSliding && moveDirection == 0 && canJump) { isWallSliding = false; amountOfJumpsLeft--; Vector2 forceToAdd = new Vector2(wallHopForce * wallHopDirection.x * -facingDirection, wallHopForce * wallHopDirection.y); rb2D.AddForce(forceToAdd, ForceMode2D.Impulse); } // Below is for Jump at Wall else if ((isWallSliding || isTouchingWall) && moveDirection != 0 && canJump) { isWallSliding = false; amountOfJumpsLeft--; Vector2 forceToAdd = new Vector2(wallJumpForce * wallJumpDirection.x * moveDirection, wallJumpForce * wallJumpDirection.y); rb2D.AddForce(forceToAdd, ForceMode2D.Impulse); } } ``` 3. Set JumpIf Condition ```csharp= private void CheckIfJump() { // isWallSlipding => canJump state if ((isGrounded && rb2D.velocity.y <= 0) || isWallSliding) amountOfJumpsLeft = amountOfJumps; ~~~ ``` 4. Wall Face Direction setting(For Hop) ```csharp= private int facingDirection = 1; protected void Flip() { if (!isWallSliding) // This condition for right position for wallSliding. { facingDirection *= -1; isFacingRight = !isFacingRight; transform.Rotate(0.0f, 100.0f, 0.0f); } } ```