Unity Platform 05 Additional Jump
===
2020.08.11
###### tags: `platform`
## List
1. [Additional Jump Technics](#AJT)
## Content
### Additional Jump Technics<a id="AJT"/>
<b>*Apply Jump*</b> is most technical movement. Double jump, wall jump, jump delay...<br>We make some addtional jump through next steps.
1. Delete Air Drag Move
This is no need for us.<br>
**Before**
```csharp=
protected void applyMovement()
{
if (isGrounded)
rb2D.velocity = new Vector2(velocity * moveDirection, rb2D.velocity.y);
// This for Charactor while jump, move little different.
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);
}
// This for slow down airmove when stop input. move speed slow down.
else if (!isGrounded && !isWallSliding && moveDirection == 0)
{
rb2D.velocity = new Vector2(rb2D.velocity.x * airDragMultiplier, rb2D.velocity.y);
}
~~~
}
```
**After**
```csharp=
protected void applyMovement()
{
// This for slow down airmove when stop input. move speed slow down.
if (!isGrounded && !isWallSliding && moveDirection == 0)
{
rb2D.velocity = new Vector2(rb2D.velocity.x * airDragMultiplier, rb2D.velocity.y);
}
else
{
rb2D.velocity = new Vector2(velocity * moveDirection, rb2D.velocity.y);
}
~~~
}
```
2. Jump Option splitted & Edit CheckIfJump
Wall Jump and Normal Jump splitted. Condition_Check is also splitted
**Before**
```csharp=
private bool canJump;
private void CheckIfJump()
{
if ((isGrounded && rb2D.velocity.y <= 0) || isWallSliding)
amountOfJumpsLeft = amountOfJumps;
~~~
}
```
**After**
```csharp=
private bool canNormalJump;
private bool canWallJump;
private void CheckIfJump()
{
if (isGrounded && rb2D.velocity.y <= 0.01f)
amountOfJumpsLeft = amountOfJumps;
if (isTouchingWall)
canWallJump = true;
~~~
}
```
3. Edit Jump
Split Jump to Wall, Slide jump function.<br>Must Check conditions are right.
**Before**
```csharp=
protected void CheckInput()
{
moveDirection = Input.GetAxisRaw("horizontal");
if (Input.GetButtonDown("Jump"))
Jump();
// This for change jump tab height;
if (Input.GetButtonUp("Jump"))
rb2D.velocity = new Vector2(rb2D.velocity.x, rb2D.velocity.y * jumpHighierMultiplier);
}
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);
}
}
```
**After**
```csharp=
public float jumpTimerSet = 0.15f;
private bool isAttemptingToJump;
private float jumpTimer;
protected void CheckInput()
{
~~~
if (Input.GetButtonDown("Jump"))
{
if (isGrounded || (amountOfJumpsLeft > 0 && isTouchingWall))
NormalJump();
else
{
jumpTimer = jumpTimerSet;
isAttemptingToJump = true;
}
}
~~~
}
protected void CheckJump()
{
if (jumpTimer > 0)
{
//wall Jump
if (!isGrounded && isTouchingWall && moveDirection != 0 && moveDirection != facingDirection)
{
WallJump();
}
else if (isGrounded)
{
NormalJump();
}
}
if (isAttemptingToJump)
{
jumpTimer -= Time.deltaTime;
}
}
protected void NormalJump()
{
if (canNormalJump) // !isWallSliding is wall jump
{
rb2D.velocity = new Vector2(rb2D.velocity.x, jumpForce);
amountOfJumpsLeft--;
jumpTimer = 0;
isAttemptingToJump = false;
}
}
protected void WallJump()
{
// Below is for Jump at Wall
if (canWallJump)
{
rb2D.velocity = new Vector2(rb2D.velocity.x, 0.0f);
isWallSliding = false;
amountOfJumpsLeft = amountOfJumps;
amountOfJumpsLeft--;
Vector2 forceToAdd = new Vector2(wallJumpForce * wallJumpDirection.x * moveDirection, wallJumpForce * wallJumpDirection.y);
rb2D.AddForce(forceToAdd, ForceMode2D.Impulse);
jumpTimer = 0;
isAttemptingToJump = false;
}
}
```
4. Edit CheckIfWallSliding
Condition little bit is editted.
```csharp=
private void CheckIfWallSliding()
{
if (isTouchingWall && moveDirection == facingDirection)
isWallSliding = true;
else
isWallSliding = false;
}
```
6. Tab off Jump Additionality
Before, use `ButtonUp()` function, now, use `checkJumpMultiplier`
```csharp=
if (checkJumpMultiplier && !Input.GetButton("Jump"))
{
checkJumpMultiplier = false;
rb2D.velocity = new Vector2(rb2D.velocity.x, rb2D.velocity.y * jumpHighierMultiplier);
}
```
7. Move & Flip Error Fix
Turn error be fixed with `canMove` and `canFlip` and `turnTimer`<br>Write below
```csharp=
if (Input.GetButtonDown("horizontal") && isTouchingWall)
{
if (!isGrounded && moveDirection != facingDirection)
{
canMove = false;
canFlip = false;
turnTimer = turnTimerSet;
}
}
if (!canMove)
{
turnTimer -= Time.deltaTime;
if (turnTimer <= 0)
{
canMove = true;
canFlip = true;
}
}
```
8. Wall Jump Smooth with timer
Wall Jump aslo has timer for its move.<br>Apply below.
```csharp=
private float wallJumpTimer;
private float wallJumpTimerSet = 0.5f;
private bool hasWallJumped;
private int lastWallJumpDirection;
protected void WallJump()
{
// Below is for Jump at Wall
if (canWallJump)
{
~~~
hasWallJumped = true;
wallJumpTimer = wallJumpTimerSet;
lastWallJumpDirection = -facingDirection;
}
}
protected void CheckJump()
{
~~~
/*
* For Wall Jump again
*/
if (wallJumpTimer > 0)
{
if (hasWallJumped && moveDirection == -lastWallJumpDirection)
{
rb2D.velocity = new Vector2(rb2D.velocity.x, 0.0f);
hasWallJumped = false;
}
else if (wallJumpTimer <= 0)
{
hasWallJumped = false;
}
else
{
wallJumpTimer -= Time.deltaTime;
}
}
}
```