Forum begins after the advertisement:


[Part 8] if you hit another wall during a wall jump you get stuck flipped

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 8] if you hit another wall during a wall jump you get stuck flipped

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #15691
    James
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Hey! Heres a fun one. if while you are wall jumping you come into contact with another wall it seems like you get flipped permanently. https://imgur.com/a/koOIYlA – gif of issue

    my wall jump code, lmk if you need any other code

    <code>    void WallJump()
        {
            if (isWallSliding)
            {
                isWallJumping = false;
                wallJumpingDirection = !pState.lookingRight ? 1 : -1;
    
                CancelInvoke(nameof(StopWallJumping));
            }
            if(Input.GetButtonDown("Jump") && isWallSliding)
            {
                isWallJumping = true;
                rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
    
                dashed = false;
                airJumpCounter = 0;
    
                pState.lookingRight = !pState.lookingRight;
                transform.eulerAngles = new Vector2(transform.eulerAngles.x, 180);
    
                Invoke(nameof(StopWallJumping), wallJumpingDuration);
    
            }
        }
    
        void StopWallJumping()
        {
            isWallJumping = false;
            transform.eulerAngles = new Vector2(transform.eulerAngles.x, 0);
        }</code>

    As a note I tried addding the update found here: https://blog.terresquall.com/community/topic/general-issue-with-jumping-walljumping-bat-and-some-questions-2/ that added the jumpRotation float but after doing that when trying to wall jump from left hand walls it made it so that I didn’t jump away, just up so I reverted.

    #15697
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Okay, after testing for a bit, I’ve found the issue.

    First, the code that I gave, in the link you provided, is for a different case where there was a different Flip() method code. I presume that your Flip() method uses the localScale of the Player. However, in Part 6 of the series, we actually did change the Flip() code for the Cinemachine camera, to use eulerAngles instead of the scale.

    In other words, don’t worry, your code here is fine if you’re using the scale for Flip().


    The issue lies within the first if statement in WallJump().

            if (isWallSliding)
            {
                isWallJumping = false;
                wallJumpingDirection = !pState.lookingRight ? 1 : -1;
    
                CancelInvoke(nameof(StopWallJumping));
            }

    That one line cancelling StopWallJumping() is intended to allow the player to cling to a wall that they reach in the middle of a current walljump.

    The issue with this, is with the way that we’ve made the flipping of the player in WallJump(). We flip the player 180 using their eulerAngles, which works but we need to reset the eulerAngles again once the jump is completed. Thus, we reset it in StopWallJumping(), back to 0.

    But with that first statement, we stop the process of resetting the player’s eulerangles, and thus the player is reversed completely.


    So there’s two things you can try.

    1. Reset the Player’s eulerAngles by calling the same code in StopWallJumping() in that first if statement. [Just drop this code into the statement]: transform.eulerAngles = new Vector2(transform.eulerAngles.x, 0);

    or 2) You can flip the player using localScale instead of eulerAngles in the WallJump().

    #15704
    James
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    hey theanks for looking into this. Interestingly I already have “transform.eulerAngles = new Vector2(transform.eulerAngles.x, 0);” in my stop wall jump.

    <code>    void StopWallJumping()
        {
            isWallJumping = false;
            transform.eulerAngles = new Vector2(transform.eulerAngles.x, 0);
        }</code>

    is it maybe related to my wall slide code?

    <code> void WallSlide()
        {
            if(Walled() && !Grounded() && xAxis != 0)
            {
                isWallSliding = true;
                rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
            }
            else
            {
                isWallSliding = false;
            }
        }</code>

    adding ‘transform.eulerAngles = new Vector2(transform.eulerAngles.x, 0);’ to the iswallsliding = true section here seems to fix it but maybe that will mess with something later on?

    Cheers :)

    #15705
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Don’t worry your suggested fix will work, I’ve already explained it in my above comment, but TLDR;

    The code in StopWallJumping() is correct, but everytime you touch a wall, you stop the method from continuing. In other words, you stop the code that is fixing your player’s rotation whenever you touch a wall while still jumping.

    So, when you applied your fix to WallSlide(), you automatically fix the rotation, so long as you touched the wall, which is fine.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: