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;
}
}