I have this integer that I want to raise by say 1 every two seconds. The catch is that these two seconds MUST be spent with a collider within a trigger. If the collider leaves the trigger, than the timer is reset. This also must be contained within an if statement. I can’t think of anyway to do this with Time.time as that starts at the beginning of the scene and can’t be reset. I don’t think you can stick an IEnumerator in an if statement either; at least not without that IEnumerator looping. Any ideas?
OnTriggerStay acts like a loop, like update and quits when the collider leaves.
Do you happen to know how I could count the time within the OnTriggerStay with this timer ending when OnTriggerStay is no longer being called?
Something along the lines of this:
public float TickDuration = 1f;
public int TickCount = 0;
private float _enterTime;
private void OnTriggerEnter(Collider other)
{
_enterTime = Time.time;
}
private void OnTriggerStay(Collider other)
{
if(Time.time - _enterTime > this.TickDuration)
{
_enterTime = Time.time;
this.TickCount++;
Debug.Log("TICK");
}
}
You’ll probably want to add tests to make sure that the collider is the collider you’re concerned with. Maybe tag it with a special tag, or name, or something.