InvokeRepeating doesn't react

The Time, how long the Player was in the Air, should be recorded using the int AirTime Variable.

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 Reply

Have you tried putting a Debug.Log in the AddAirTime method to see if it fires?

Check if the AirTime value changes. Perhaps something is resetting it to 0?

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.

The last Thing I need before the Calculations are correct, is a Way to keep the AirTime resetting to 0 as long as I stay on the Ground. 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()
{
AirTime = AirTime + Time.deltaTime;
//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");
AirTime = 0; //Reset Air Time
}
}
}

Is there something between OnTriggerEnter2D and OnTriggerExit2D, that's called as long as both Triggers keep Collided with each other (Just like Update, except it stops if both Triggers are no Longer collided with each other)?

Couldn't you use a flag to keep track of that? Set a bool field to true when you leave the ground and to false when you hit the ground, then in Update increment the timer if the field is true and zero it if the field is false.

Your answer

Hint: You can notify a user about this post by typing @username

Attachments:
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.