wall jumping problem

Hi i wanna implement a wall jumping feature in my 2d game, but i ve a little problem, i move my characther with:

private void setMovement(float fInputX)
    {
        _oPlayerRB.velocity = new Vector2(fInputX * PLAYER_SPEED * Time.deltaTime, _oPlayerRB.velocity.y);
        if (fInputX != 0 && _eColliderState == _tPlayerState.IDLE)
        {
            _eColliderState = _tPlayerState.RUN;
            _oCollider.size = new Vector2(_oColliderSizeIdle.x + 0.07f, _oColliderSizeIdle.y);
            _oCollider.offset = new Vector2(_oColliderOffsetIdle.x + 0.13f,_oColliderOffsetIdle.y);
        }
        else if (fInputX == 0 && _eColliderState == _tPlayerState.RUN) {

            _eColliderState = _tPlayerState.IDLE;
            _oCollider.size = _oColliderSizeIdle;
            _oCollider.offset = _oColliderOffsetIdle;
        }
        if (fInputX > 0 && _bFlipPlayer)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            _bFlipPlayer = false;
        }
        else if (fInputX < 0 && !_bFlipPlayer)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
            _bFlipPlayer = true;
        }
    }

and the code of walljump is:

private void handleWallJump() {
        if (_bJumpButton) {
            _bIsWallJumping = true;
            playerWallJump();
        }
    }

    private void playerWallJump() {
        if (_bFlipPlayer) {
            _oPlayerRB.velocity = new Vector2(WALL_JUMP_STEP_X, JUMP_STEP);
        }
        else
        {
            _oPlayerRB.velocity = new Vector2(-WALL_JUMP_STEP_X, JUMP_STEP);
        }
    }

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

up

Where do you call “handleWallJump”?

That’ s my fixedupdate with a state machine

private void FixedUpdate()
    {
        _bGrounded = isGrounded();
        setAnimator();
        handleJump();
        handleWallSliding();
        switch (_ePlayerState)
        {
            case _tPlayerState.IDLE:

                handleDash();
                if (!_bGrounded)
                    _ePlayerState = _tPlayerState.JUMP;
                else if (_fInputX != 0)
                    _ePlayerState = _tPlayerState.RUN;
                else if (_bAttackButton)
                {
                    _ePlayerState = _tPlayerState.ATK;
                    handleAttack();
                }
                else {
                    setMovement(_fInputX);
                }
                break;

            case _tPlayerState.RUN:
                if (!_bGrounded)
                    _ePlayerState = _tPlayerState.JUMP;
                else if (_fInputX == 0)
                    _ePlayerState = _tPlayerState.IDLE;
                else if (_bAttackButton)
                {
                    handleAttack();
                    _ePlayerState = _tPlayerState.ATK;
                }
                else {
                    setMovement(_fInputX);
                    handleDash();
                }
                break;

            case _tPlayerState.JUMP:
                if (_bGrounded)
                {
                    _bCoolDownAttackJump = false;
                    _ePlayerState = _tPlayerState.IDLE;
                }
                else if (_bIsWallSliding) {
                    _bCoolDownAttackJump = false;
                    _ePlayerState = _tPlayerState.WALL_SLIDING;
                }
                else if (_bAttackButton && !_bCoolDownAttackJump)
                {
                    _bCoolDownAttackJump = true;
                    handleAttack();
                    _ePlayerState = _tPlayerState.ATK;
                }
                else
                    setMovement(_fInputX);

                handleDash();
                break;

            case _tPlayerState.ATK:
                handleAttack();
                _ePlayerState = _tPlayerState.IDLE;
                break;

            case _tPlayerState.WALL_SLIDING:
                if (_bGrounded || !_bIsWallSliding)
                {
                    _bIsWallSliding = false;
                    _ePlayerState = _tPlayerState.IDLE;
                }
                else if (!_bGrounded && !_bIsWallSliding && _oPlayerRB.velocity.y > 0)
                {
                    _ePlayerState = _tPlayerState.JUMP;
                }
                else
                {
                    setMovement(_fInputX);
                    handleWallJump();
                }
                break;
        }

        resetInputs();
    }

When _bIsWallJumping == true you are no longer “WALL_SLIDING”" so you should exit that state.
For instance:

case _tPlayerState.WALL_SLIDING:
  if(_bIsWallJumping)
  {
     if(condition)
         state=JUMP
    else if (hurtCondition)
         state=hurt
    //....                              
  }else
/*
*
* CHUNK OF CODE
*
*/

Just a quick tip: If i were you I would handle the transitions outside the logic of each state :slight_smile:

void StatesOrWhateverItsCalled()
{
 //Check Transitions and if a transition verifies then change state
 
 switch(states)
 {
 case RoamForum:
       action();
  break;
 //....
}

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?