Boolean check time in collider
Hi, I'm new to unit, I was thinking if it is possible to check how long a collider is in a trigger collider?
For example:
Collider 1 is less then 1 second in collider 2(trigger). If less then 1 sec then score -1, if longer then 2 sec do nothing.
Answer by Tourist · Feb 15, 2017 at 02:33 PM
Yes it is possible but you need to write some code.
OnTriggerEnter : set a float value to 0
OnTriggerStay : increment the float value with Time.deltaTime and do your stuff if needed
@Tourist I got the following code, but it doesn't work at this stage. The DecrementScore() instance is just linked to a score--
void OnTriggerStay (Collider target) {
if (didJump) {
if (target.tag == "Platform") {
stayTime = Time.time;
if (stayTime < 4f)
Score$$anonymous$$anager.instance.DecrementScore ();
}
}
}
private void OnTriggerExit(Collider target)
{
if (target.tag == "Platform")
isGrounded = false;
ResetTimer();
}
void ResetTimer ()
{
stayTime = 0.0f;
}
You re just assigning stayTime to the current time. Look at the documentation about Time.time.
you should set stayTime to 0 on enter and increment time with deltaTime as explained above. Time.time is the time since the beginning of the game.