- Home /
InvokeRepeating doesn't react
https://youtu.be/3FPO72a9N3A
So, I wanted to Implement so-called ,,Coyote Time".
I planned the Script to go something like this:
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
}
}
Answer by LCStark · Oct 01, 2018 at 05:04 PM
Have you tried putting a
Debug.Log
in theAddAirTime
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 yourAirTime
inUpdate
should be enough to track air time, instead of complicatedInvokedRepeating
maneuvers. Or, you could useFixedUpdate
and increment byTime.fixedDeltaTime
.
Thank you! I've come way closer to my Destination. Now, the Counter works.
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 : $$anonymous$$onoBehaviour
{
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)?
https://docs.unity3d.com/ScriptReference/Collider2D.OnTriggerStay2D.html
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
Follow this Question
Related Questions
Unity Hub not opening my project 0 Answers
Why is InvokeRepeating not working in boolean method? 0 Answers
,Enemy AI rapidfires and invokerepeating doesn't do anything. c# 1 Answer
Unity 5.5.2f1 crashes after creating new project or loading existing project 1 Answer
Can't click on Input fields. 3 Answers