- Home /
Counter for double jump don't work
Counter skips to 3 instead of 0,1,2. I'm making a double jump, so I put a jump counter that gets increment by 1 every time the player jumps. however when I tested it jumps straight to 3, the more strange is that very rarely it works as intended.
I've tried a lot of things like making that the game checks for three instead of 2, but then it jumps the count to 5...
My code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Walking : MonoBehaviour
{
public bool inGrou;
private float jumpCount;
private float maxJump;
public float speedW = 1f;
public float speedJ = 3f;
private Rigidbody2D rig;
void Start()
{
rig = GetComponent<Rigidbody2D>();
//inGrou = true;
maxJump = 2f;
jumpCount = 0f;
Debug.Log($"1 Jump count = {jumpCount} e maxJump = {maxJump}");
}
// Update is called once per frame
void Update()
{
// float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
Debug.Log($"2 Jump count = {jumpCount} e maxJump = {maxJump}");
Vector2 pos = transform.position;
pos.x += h * speedW * Time.deltaTime;
if(Input.GetKey("w") && jumpCount <= maxJump)
{
rig.velocity = Vector2.up * speedJ;
jumpCount++;
Debug.Log($"3 Jump count = {jumpCount} e maxJump = {maxJump}");
}
if(Input.GetKey("a") || Input.GetKey("d"))
{
transform.position = pos;
}
}
void OnCollisionEnter2D(Collision2D colisor)
{
jumpCount = 0;
}
}
Answer by Eno-Khaon · Oct 03, 2021 at 03:29 AM
It's less that it's skipping values and more that it's just cycling through them too rapidly.
Input.GetKey() triggers on every frame the key is held.
Input.GetKeyDown() would trigger only on the first frame that the key is pressed.
Your answer
Follow this Question
Related Questions
I need help with adding double jump to my player movement script 0 Answers
Jumps never reset 2 Answers
Double Jump using First Person Controller 1 Answer
Double jump does not work why? 0 Answers