InvokeRepeating doesn't react

2018-10-01 18-02-28.mkv - YouTube

So, I wanted to Implement so-called ,Coyote Time".


I planned the Script to go something like this:

  1. The Time, how long the Player was in the Air, should be recorded using the int AirTime Variable.
  2. If the Player is in the Air for the Right Amount of Time, a Second Ground Check behind the Main Ground Check should appear.

It’s proven that the
OnTriggerEnter2D(){}
and
OnTriggerExit2D(){}
Parts work, thanks to the
Debug.Log(); Lines,
but the InvokeRepeating(); somehow doesn’t work.
I can’t understand, why the InvokeRepeating(); doesn’t react at all, because the Rest of the Code in OnTriggerEnter2D(){} (and even OnTriggerExit2D(){} ) works.
Normally, the int AirTime Variable (See Video) should go up, as long as the Player is in the Air, and should only stay at 0, as long as the Player is on the Ground.
Please take a look at my Script and hopefully you can tell me what’s wrong.
Before telling me your Solution, please keep in Mind, that I want the Calculations for the AirTime Variable to be Frame-Rate-Independent, if possible.
If you’ve told me a Solution, that’s not Frame-Rate independent, and then found a Solution, that’s actually Frame-Rate Independent, please come back and Answer with the Frame-Rate Independent Solution.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AirJumpPeriod : MonoBehaviour
{

    public float AirTime = 0; //How long you've been in the Air and not on the Ground. Will be resetted whenever you go to the Ground again.
    public GameObject AnotherGroundCheck;

    // Start is called before the first frame update
    void Start()
    {
        //InvokeRepeating("AddAirTime", 0, 0.1f); //Add Air Time every 10th of a Second
    }

    // Update is called once per frame
    void Update()
    {
        //If you've been in the Air for at least 0.01 Seconds, but also for less than 2 Seconds:
        if (AirTime > 0.01f && AirTime < 2f)
        {
            AnotherGroundCheck.SetActive(true); //Activate the other Ground Check
        } else //Otherwise
        {
            AnotherGroundCheck.SetActive(false); //Deactivate the other Ground Check
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        //When you hit Ground
        if (other.CompareTag("Ground"))
        {
            Debug.Log("Got on Ground");

            CancelInvoke("AddAirTime");
            AirTime = 0; //Reset Air Time
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        //When you leave Ground
        if (other.CompareTag("Ground"))
        {
            Debug.Log("Left Ground");

            //RepeatAddingAirTime(); //Add Air Time every 100th of a Second
            InvokeRepeating("AddAirTime", 0f, 0.01f); //Add Air Time every 100th of a Second
        }
    }

    /*void RepeatAddingAirTime()
    {
        InvokeRepeating("AddAirTime", 0f, 0.001f); //Add Air Time every 1000th of a Second
    }*/

    void AddAirTime()
    {
        AirTime =+ 0.001f; //Add 0.001 Air Time
    }
}
  1. Have you tried putting a Debug.Log in the AddAirTime method to see if it fires?
  2. Check if the AirTime value changes. Perhaps something is resetting it to 0?
  3. I think that simply adding Time.deltaTime to your AirTime in Update should be enough to track air time, instead of complicated InvokedRepeating maneuvers. Or, you could use FixedUpdate and increment by Time.fixedDeltaTime.