Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

wall jumping problem

Discussion in '2D' started by Zephyros97, Jul 28, 2019.

  1. Zephyros97

    Zephyros97

    Joined:
    Sep 14, 2015
    Posts:
    19
    Hi i wanna implement a wall jumping feature in my 2d game, but i ve a little problem, i move my characther with:


    Code (CSharp):
    1. private void setMovement(float fInputX)
    2.     {
    3.         _oPlayerRB.velocity = new Vector2(fInputX * PLAYER_SPEED * Time.deltaTime, _oPlayerRB.velocity.y);
    4.         if (fInputX != 0 && _eColliderState == _tPlayerState.IDLE)
    5.         {
    6.             _eColliderState = _tPlayerState.RUN;
    7.             _oCollider.size = new Vector2(_oColliderSizeIdle.x + 0.07f, _oColliderSizeIdle.y);
    8.             _oCollider.offset = new Vector2(_oColliderOffsetIdle.x + 0.13f,_oColliderOffsetIdle.y);
    9.         }
    10.         else if (fInputX == 0 && _eColliderState == _tPlayerState.RUN) {
    11.  
    12.             _eColliderState = _tPlayerState.IDLE;
    13.             _oCollider.size = _oColliderSizeIdle;
    14.             _oCollider.offset = _oColliderOffsetIdle;
    15.         }
    16.         if (fInputX > 0 && _bFlipPlayer)
    17.         {
    18.             transform.eulerAngles = new Vector3(0, 0, 0);
    19.             _bFlipPlayer = false;
    20.         }
    21.         else if (fInputX < 0 && !_bFlipPlayer)
    22.         {
    23.             transform.eulerAngles = new Vector3(0, 180, 0);
    24.             _bFlipPlayer = true;
    25.         }
    26.     }
    and the code of walljump is:


    Code (CSharp):
    1. private void handleWallJump() {
    2.         if (_bJumpButton) {
    3.             _bIsWallJumping = true;
    4.             playerWallJump();
    5.         }
    6.     }
    7.  
    8.     private void playerWallJump() {
    9.         if (_bFlipPlayer) {
    10.             _oPlayerRB.velocity = new Vector2(WALL_JUMP_STEP_X, JUMP_STEP);
    11.         }
    12.         else
    13.         {
    14.             _oPlayerRB.velocity = new Vector2(-WALL_JUMP_STEP_X, JUMP_STEP);
    15.         }
    16.     }
    obv the edit of velocity like this way have some problem with the x, who is continuously replaced, what s the best way for handle this? (The movement is almost always active) Ty for helping me <3
     
  2. Zephyros97

    Zephyros97

    Joined:
    Sep 14, 2015
    Posts:
    19
  3. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    229
    Where do you call "handleWallJump"?
     
  4. Zephyros97

    Zephyros97

    Joined:
    Sep 14, 2015
    Posts:
    19
    That' s my fixedupdate with a state machine

    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         _bGrounded = isGrounded();
    4.         setAnimator();
    5.         handleJump();
    6.         handleWallSliding();
    7.         switch (_ePlayerState)
    8.         {
    9.             case _tPlayerState.IDLE:
    10.  
    11.                 handleDash();
    12.                 if (!_bGrounded)
    13.                     _ePlayerState = _tPlayerState.JUMP;
    14.                 else if (_fInputX != 0)
    15.                     _ePlayerState = _tPlayerState.RUN;
    16.                 else if (_bAttackButton)
    17.                 {
    18.                     _ePlayerState = _tPlayerState.ATK;
    19.                     handleAttack();
    20.                 }
    21.                 else {
    22.                     setMovement(_fInputX);
    23.                 }
    24.                 break;
    25.  
    26.             case _tPlayerState.RUN:
    27.                 if (!_bGrounded)
    28.                     _ePlayerState = _tPlayerState.JUMP;
    29.                 else if (_fInputX == 0)
    30.                     _ePlayerState = _tPlayerState.IDLE;
    31.                 else if (_bAttackButton)
    32.                 {
    33.                     handleAttack();
    34.                     _ePlayerState = _tPlayerState.ATK;
    35.                 }
    36.                 else {
    37.                     setMovement(_fInputX);
    38.                     handleDash();
    39.                 }
    40.                 break;
    41.  
    42.             case _tPlayerState.JUMP:
    43.                 if (_bGrounded)
    44.                 {
    45.                     _bCoolDownAttackJump = false;
    46.                     _ePlayerState = _tPlayerState.IDLE;
    47.                 }
    48.                 else if (_bIsWallSliding) {
    49.                     _bCoolDownAttackJump = false;
    50.                     _ePlayerState = _tPlayerState.WALL_SLIDING;
    51.                 }
    52.                 else if (_bAttackButton && !_bCoolDownAttackJump)
    53.                 {
    54.                     _bCoolDownAttackJump = true;
    55.                     handleAttack();
    56.                     _ePlayerState = _tPlayerState.ATK;
    57.                 }
    58.                 else
    59.                     setMovement(_fInputX);
    60.  
    61.                 handleDash();
    62.                 break;
    63.  
    64.             case _tPlayerState.ATK:
    65.                 handleAttack();
    66.                 _ePlayerState = _tPlayerState.IDLE;
    67.                 break;
    68.  
    69.             case _tPlayerState.WALL_SLIDING:
    70.                 if (_bGrounded || !_bIsWallSliding)
    71.                 {
    72.                     _bIsWallSliding = false;
    73.                     _ePlayerState = _tPlayerState.IDLE;
    74.                 }
    75.                 else if (!_bGrounded && !_bIsWallSliding && _oPlayerRB.velocity.y > 0)
    76.                 {
    77.                     _ePlayerState = _tPlayerState.JUMP;
    78.                 }
    79.                 else
    80.                 {
    81.                     setMovement(_fInputX);
    82.                     handleWallJump();
    83.                 }
    84.                 break;
    85.         }
    86.  
    87.         resetInputs();
    88.     }
     
  5. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    229
    When _bIsWallJumping == true you are no longer "WALL_SLIDING"" so you should exit that state.
    For instance:
    Code (CSharp):
    1. case _tPlayerState.WALL_SLIDING:
    2.   if(_bIsWallJumping)
    3.   {
    4.      if(condition)
    5.          state=JUMP
    6.     else if (hurtCondition)
    7.          state=hurt
    8.     //....                              
    9.   }else
    10. /*
    11. *
    12. * CHUNK OF CODE
    13. *
    14. */
    15.  
    Just a quick tip: If i were you I would handle the transitions outside the logic of each state :)

    Code (CSharp):
    1.  
    2. void StatesOrWhateverItsCalled()
    3. {
    4.  //Check Transitions and if a transition verifies then change state
    5.  
    6.  switch(states)
    7.  {
    8.  case RoamForum:
    9.        action();
    10.   break;
    11.  //....
    12. }
    13.  
     
    Last edited: Jul 30, 2019
  6. Zephyros97

    Zephyros97

    Joined:
    Sep 14, 2015
    Posts:
    19
    okok i ll do!, but this is not the solution on my problem xD, i move my characther editing the velocity, so when i wall jump i add a force opposite to the wall to my player, but the velocity is replaced obviously, so what can i do?