Forum begins after the advertisement:


[Part 1-2 Bug Fixes] Faulty Double Jump, Jump & Dash, Jittery Sprites

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 1-2 Bug Fixes] Faulty Double Jump, Jump & Dash, Jittery Sprites

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #13608
    Joseph Tang
    Moderator
    Helpful?
    Up
    1
    ::

    This is a supplementary post written for Part 1 & 2 of our Metroidvania series, and it aims to address 1 things:

    1. Address common issues / questions that readers run into, and possible solutions for these questions.

    For convenience, below are the links to the article and the video:

    [Part 1]

    [Part 2]


    Table of Contents

    [Part 1]

    1. Player is not Jumping
    2. The Camera/Player is jittering/glitching

    [Part 2]

    1. Dashing mechanic does not work.
    2. Double Jump is not working.
    3. Dashing when left-clicking/attacking [Part 3 mechanic].
    4. Jump Buffer with adaptive frame rate

    [Part 1] Movement & Camera

    1. Player is not Jumping

    • The Layer for whatIsGround has not been set on the Player. Or the Floor has not been assigned the same Layer as whatIsGround.
    • If the values of GroundCheck X & GroundCheck Y are not big enough.
    • Ground Check Point gameObject is not assigned or is not low enough, relative to the Player with GroundCheck Y value included, to reach the Layer Ground beneath the player.
    Set your Floor gameobject to the same Layer as ‘What Is Ground’.
    Set an empty gameobject to the ‘Ground Check Point’.
    Ensure the Ground Check X & Y values are big enough for them to reach the floor.
    Set What Is Ground Layer to an appropriate Layer.
    Ensure your empty gameobject used for the Ground Check Point is low enough/below the player’s model and box collider.

    2. The Camera/Player is jittering/glitching.

    • Set Player’s RigidBody2D to Interpolate
    Set Player’s RigidBody2D to Interpolate to prevent jittering.

    [Interpolation solves most jittering issues, but when the player is moving at a fast enough speed, we can see the Player jitter backwards a few frames here and there. This is caused by the Camera not being able to match the Player’s speed.]

    • Set Player’s walkSpeed to a lower value
    • Or set Camera’s followSpeed to a higher value/[1]

    Ensure the Walk Speed is not too fast.

    Ensure the Camera’s Follow Speed is fast enough.

    Note: Setting Camera’s followSpeed to [1] will guarantee the Camera will follow the Player exactly.


    [Part 2] Dash, Double Jump & Advanced Movement.

    1. Dashing mechanic does not work.

    [This issue is most commonly caused by a missing piece of code]

    • Ensure that all parts of the Dashing mechanic’s code in the PlayerController script is as follows:
        private bool canDash = true;
        private bool dashed;
    
        void Update()
        {
            GetInputs();
            UpdateJumpVariables();
    
            if (pState.dashing) return;
            Flip();
            Move();
            Jump();
            StartDash();
        }
    
        void StartDash()
        {
            if (Input.GetButtonDown("Dash") && canDash && !dashed)
            {
                StartCoroutine(Dash());
                dashed = true;
            }
            if (Grounded())
            {
                dashed = false;
            }
        }
    
        IEnumerator Dash()
        {
            canDash = false;
            pState.dashing = true;
            anim.SetTrigger("Dashing");
            rb.gravityScale = 0;
            rb.velocity = new Vector2(transform.localScale.x * dashSpeed, 0);
            if (Grounded()) Instantiate(dashEffect, transform);
            yield return new WaitForSeconds(dashTime);
            rb.gravityScale = gravity;
            pState.dashing = false;
            yield return new WaitForSeconds(dashCooldown);
            canDash = true;
        }

    2. Double Jump is not working.

    [While this can be caused by missing code, the actual code for the mechanic relies on variables that can never fire if the value is left at [0]]

    • Ensure all the values for Double Jump in the Inspector are set.

    Ensure that all values are above [0].

    3. Dashing when left-clicking/attacking [Part 3 mechanic].

    • Remove the “Mouse 0” keybind from “Alt Positive Button” under the Dash input from the Input Manager.

    Remove any input under Alt Positive Button.

    4. Jump Buffer with adaptive frame rate.

    [A suggested fix for high frame rates to handle the low frame count of our jump buffer, by viewer VoxBeats]

        private int float jumpBufferCounter = 0;
        [SerializeField] private int float jumpBufferFrames;
    
        void UpdateJumpVariables()
        {
            ...
    
            if (Input.GetButtonDown("Jump"))
            {
                jumpBufferCounter = jumpBufferFrames;
            }
            else
            {
                jumpBufferCounter--;
                jumpBufferCounter = jumpBufferCounter - Time.deltaTime * 10;
            }
        }

    That will be all for Part 1 & 2. I will soon be updating on the common issues for Part 3.
    Hopefully this can help you on any issues you may have. However, if you find that your issues weren’t addressed or is a unique circumstance, you can submit a forum post to go into detail on your problem for further assistance.

    #15181
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    Just wanted to add an extra note for Part 1 pointed out by @soulhealer6154 on YouTube:

    Flipping non-uniformly scaled character sprites

    If you have a character sprite that does not have a scale of (1, 1, 1), you may find that when moving the character around, your sprite’s X, Y and Z values will always change to 1. This may distort your sprite.

    To fix this, you will need to modify the Flip() function on PlayerController as follows:

    void Flip()
    {
        if (xAxis < 0)
        {
            transform.localScale = new Vector2(-1 -Mathf.Abs(transform.localScale.x), transform.localScale.y);
        }
        else if (xAxis > 0)
        {
            transform.localScale = new Vector2(1 Mathf.Abs(transform.localScale.x), transform.localScale.y);
        }
    }

    What the code does is that instead of setting the scale to 1, we are using the GameObject’s current scale value, but using Mathf.Abs() on it to make sure that we always take the positive value. Then, when moving left, we take -Maths.Abs(), so that we always take the negative of the value. Of course, for the right-facing movement, we just take the default (positive) value.

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

Go to Login Page →


Advertisement below: