Help on my coyote timing

I was making a 2D platformer prototype code. I tried to add coyote timing so many times on the code but it always caused many different bugs (i.e infinite jump window, triple jump, not being able to jump and etc.). So I’ve decided to make something else - now the code is a lot more complete, but even then I can’t use coyote timing. Right now, my double jump is only working a few moments after the jump. What should I do?
(Notes: if you have any overall hints on how to improve the code, I’m taking those no problem.)

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices.ComTypes;
using DG.Tweening;
using Unity.PlasticSCM.Editor.WebApi;
using UnityEditor;

public class movimento : MonoBehaviour
{
    #region Variáveis de controle

    private bool isJumping = false;
    private bool isRunning = false;
    private bool isDashing = false;
    private bool isDashCooldown = false;
    private bool isSprintDash = false;
    private bool isFacingRight = true;
    private int jumpCount = 0;  // variável de contagem de pulo
    private const int maxJumpCount = 2; // número máximo de pulos
    #endregion

    #region Variáveis de entrada
    private bool jumpKeyPressed;
    private bool runKeyPressed;
    private bool dashKeyPressed;
    private float horizontalAxis;
    #endregion

    #region Componentes
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private TrailRenderer td;
    [SerializeField] private Transform groundCheck;
    [SerializeField] private LayerMask groundLayer;
    [SerializeField] private Transform wallCheck;
    [SerializeField] private LayerMask wallLayer;
    #endregion

    #region Parâmetros ajustáveis
    [SerializeField]
    private float forçaDoPulo = 15f;
    [SerializeField]
    private float jumpForceDecreaseFactor = 0.5f; // fator de diminuição da força do pulo
    [SerializeField]
    private float gravidade = 60f;
    [SerializeField]
    private float velocidade = 12f;
    [SerializeField]
    private float velocidadeCorrida = 18f;
    [SerializeField]
    private float transicaoVelocidade = 10f;
    [SerializeField]
    private float dashSpeed = 20f;
    [SerializeField]
    private float dashDuration = 0.2f;
    [SerializeField]
    private float dashCooldown = 0.1f;
    [SerializeField]
    private float jumpBufferLength = 0.1f;
    [SerializeField]
    private float fallSpeedMultiplier = 2f;
    [SerializeField]
    private float defaultGravityScale = 5f;
    #endregion

    #region Variáveis privadas
    private float jumpBufferCount;
    private Vector2 dashVelocity;
    private float fallTime;
    private bool isDashAvailable = true;
    private float originalDashSpeed;
    private bool isWallSliding;
    [SerializeField] private float wallSlidingSpeed = 2f;
    private bool isWallJumping;
    private float wallJumpingDirection;
    [SerializeField]
    private float wallJumpingTime = 0.2f;
    private float wallJumpingCounter;
    [SerializeField]
    private float wallJumpingDuration = 0.4f;
    [SerializeField]
    private Vector2 wallJumpingPower = new Vector2(8f, 16f);
    private bool hasAirJumped = false;  // Se o personagem já pulou no ar
    [SerializeField] private float coyoteJumpDelay = 0.2f;
    private float coyoteTimer;
    private float jumpTimer;
    [SerializeField] private float jumpDelay = 0.25f;

    #endregion

    #region Unity Callbacks
    private void Start()
    {
        if (!TryGetComponent<Rigidbody2D>(out rb))
        {
            Debug.LogError("Componente Rigidbody2D não encontrado!");
            this.enabled = false;
        }
        if (!TryGetComponent<TrailRenderer>(out td))
        {
            Debug.LogError("Componente TrailRenderer não encontrado!");
            this.enabled = false;
        }
        originalDashSpeed = dashSpeed;
    }

    private void Update()
    {
        ReadInput();
        HandleInput();
        WallSlide();
        WallJump();
        if (!isWallJumping)
        {
            Flip();
        }
        if (isGrounded() || IsWalled())
        {
            coyoteTimer = Time.time + coyoteJumpDelay;
            if (isJumping) // Se estava pulando e agora está no chão
            {
                isJumping = false;
                jumpCount = 0; // Resetamos a contagem de pulos
                hasAirJumped = false; // Resetamos o pulo no ar
            }
        }
        else
        {
            isJumping = true;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Chao"))
        {
            isJumping = false;
            isDashAvailable = true;
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Chao"))
        {
            isJumping = true;
        }
    }
    #endregion

    #region Leitura das Entradas
    private void ReadInput()
    {
        jumpKeyPressed = Input.GetKeyDown(KeyCode.Space);
        runKeyPressed = Input.GetKey(KeyCode.LeftShift); // Alterado para GetKey
        dashKeyPressed = Input.GetKeyDown(KeyCode.X);
        horizontalAxis = Input.GetAxis("Horizontal");
    }
    #endregion

    #region Lógica do jogador
    private void HandleInput()
    {
        if (jumpKeyPressed)
        {
            jumpBufferCount = jumpBufferLength;
        }

        jumpBufferCount -= Time.deltaTime;

        // Adicionamos a condição de que o jogador só pode pular se o tempo de coyote ainda não tiver passado
        if ((jumpCount < maxJumpCount && jumpBufferCount >= 0) && coyoteTimer > Time.time)
        {
            Pulo();
        }


        if (runKeyPressed) // Corre quando a tecla está pressionada
        {
            isRunning = true;
        }
        else // Para de correr quando a tecla é solta
        {
            isRunning = false;
        }

        if (!isDashing && dashKeyPressed)
        {
            if (!isJumping || (isJumping && isDashAvailable)) // Permite o dash se não estiver no ar ou se estiver no ar, mas o dash estiver disponível
            {
                StartCoroutine(Dash(horizontalAxis));
                isDashAvailable = false; // Desativa o dash após ser executado
            }
        }
    }

    private void Pulo()
    {
        // Se o jogador já fez o número máximo de pulos, retornamos
        if (jumpCount >= maxJumpCount) return;

        rb.velocity = new Vector2(rb.velocity.x, 0f);
        // A força do pulo diminui a cada pulo adicional
        rb.AddForce(Vector2.up * forçaDoPulo * (1 - jumpForceDecreaseFactor * (jumpCount - 1)), ForceMode2D.Impulse);
        isJumping = true;
        jumpBufferCount = 0;
        // Incrementamos a contagem de pulos
        jumpCount++;
    }
    private IEnumerator Dash(float direction)
    {
        float usedDashSpeed = dashSpeed;
        if (isRunning && !isSprintDash)
        {
            usedDashSpeed += ((velocidadeCorrida + velocidade) / 3);
            isSprintDash = true;
            Dash(horizontalAxis);
            Camera.main.transform.DOComplete();
            Camera.main.transform.DOShakePosition(.3f, 1.2f, 14, 90, false, true);
        }
        if (!isRunning)
            Camera.main.transform.DOComplete();
        Camera.main.transform.DOShakePosition(.2f, .5f, 14, 90, false, true);
        isDashing = true;
        isDashCooldown = true;

        rb.gravityScale = 0f;

        float dashDirection = 0f;

        if (direction < 0)
            dashDirection = -1f;
        else if (direction > 0)
            dashDirection = 1f;
        else
            dashDirection = transform.localScale.x;

        dashVelocity = new Vector2(usedDashSpeed * dashDirection, 0f);
        float endTime = Time.time + dashDuration;

        while (Time.time < endTime)
        {
            float t = (endTime - Time.time) / dashDuration;
            rb.velocity = Vector2.Lerp(rb.velocity, dashVelocity, 1f - t);
            yield return null;
        }

        rb.gravityScale = defaultGravityScale;

        isDashing = false;
        isSprintDash = false;
        dashSpeed = originalDashSpeed;

        yield return new WaitForSeconds(dashCooldown);

        isDashCooldown = false;
    }
    #endregion

    #region Física
    private void FixedUpdate()
    {
        float velocidadeAtual = isRunning ? velocidadeCorrida : velocidade;
        Vector2 velocidadeHorizontal = new Vector2(horizontalAxis * velocidadeAtual, rb.velocity.y);

        if (isDashing)
        {
            velocidadeHorizontal = dashVelocity;
        }

        rb.velocity = Vector2.Lerp(rb.velocity, velocidadeHorizontal, transicaoVelocidade * Time.fixedDeltaTime);
        if (!isJumping)
        {
            fallTime += Time.deltaTime;
            float fallSpeed = Mathf.Sqrt(fallTime) * fallSpeedMultiplier;
            if (rb.velocity.y < -fallSpeed)
            {
                rb.velocity = new Vector2(rb.velocity.x, -fallSpeed);
            }
        }
        else
        {
            fallTime = 0;
        }
        ApplyGravity();
    }

    private void ApplyGravity()
    {
        if (!isDashing)
        {
            // Aqui a gravidade é aplicada independente da direção vertical do personagem
            rb.AddForce(new Vector2(0f, -gravidade), ForceMode2D.Force);
        }
    }
    private void Flip()
    {
        if (!isFacingRight && horizontalAxis > 0f || isFacingRight && horizontalAxis < 0f)
        {
            isFacingRight = !isFacingRight;
            Vector3 localscale = transform.localScale;
            localscale.x *= -1f;
            transform.localScale = localscale;
        }
    }

    private bool isGrounded()
    {
        return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    }
    private bool IsWalled()
    {
        return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer);
    }
    private void WallSlide()
    {
        if (IsWalled() && !isGrounded() && horizontalAxis != 0f)
        {
            isWallSliding = true;
            rb.velocity = new Vector3(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
        }
        else
        {
            isWallSliding = false;
        }
    }
    private void WallJump()
    {
        if (isWallSliding)
        {
            isWallJumping = false;
            wallJumpingDirection = -transform.localScale.x;
            wallJumpingCounter = wallJumpingTime;
            CancelInvoke(nameof(StopWallJumping));
        }
        else
        {
            wallJumpingCounter -= Time.deltaTime;
        }
        if (jumpKeyPressed && wallJumpingCounter > 0f)
        {
            isWallJumping = true;
            jumpCount = 0;  // Resetamos a contagem de pulos após um wall jump
            hasAirJumped = false; // Resetamos o pulo no ar após um wall jump
            rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
            wallJumpingCounter = 0f;
            if (transform.localScale.x != wallJumpingDirection)
            {
                isFacingRight = !isFacingRight;
                Vector3 localScale = transform.localScale;
                localScale.x *= -1f;
                transform.localScale = localScale;
            }
            Invoke(nameof(StopWallJumping), wallJumpingDirection);
        }
    }
    private void StopWallJumping()
    {
        isWallJumping = false;
    }
    #endregion
}

You should begin debugging!

Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the names of the GameObjects or Components involved?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.


If you prefer, you’re welcome to look at my multi-jump coyote timing demo here:

https://www.youtube.com/watch?v=1Xs5ncBgf1M

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

https://gitlab.com/kurtdekker/proximity_buttons

https://sourceforge.net/projects/proximity-buttons/

i’m so sorry it took me this long to answer, thank you though, what you said makes sense :slight_smile: