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
- This topic has 1 reply, 2 voices, and was last updated 4 months, 1 week ago by Terence.
-
AuthorPosts
-
March 22, 2024 at 7:54 pm #13608::
This is a supplementary post written for Part 1 & 2 of our Metroidvania series, and it aims to address 1 things:
- 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]
- Article Link: https://blog.terresquall.com/2023/04/creating-a-metroidvania-like-hollow-knight-part-1/
- Video Link: https://youtu.be/dYcf9_TdEW4
[Part 2]
- Article Link: https://blog.terresquall.com/2023/05/creating-a-metroidvania-like-hollow-knight-part-2/
- Video Link: https://youtu.be/rAnVMkJjWSI
Table of Contents
[Part 1]
[Part 2]
- Dashing mechanic does not work.
- Double Jump is not working.
- Dashing when left-clicking/attacking [Part 3 mechanic].
- 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 theFloor
has not been assigned the same Layer aswhatIsGround
. - 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 withGroundCheck Y
value included, to reach the LayerGround
beneath the player.
2. The Camera/Player is jittering/glitching.
- Set Player’s RigidBody2D to Interpolate
[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]
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.
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.
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
intfloat jumpBufferCounter = 0; [SerializeField] privateintfloat 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.have upvoted this post. July 4, 2024 at 12:09 pm #15181::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 onPlayerController
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(1Mathf.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. -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: