If "grounded = false" for two seconds, "grounded = true"??

I’m having some issues with player collision and sometimes even when the player has collided with something, grounded still = false. I want to create a scenario that makes it when grounded = false for more than 2 seconds, grounded goes back to = true.

ideas? THANKS!

using UnityEngine;
using System.Collections;

public class JumpTwo : MonoBehaviour {
public bool grounded = true;
public float jumpPower = 1000;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	if (Input.GetKeyUp ("space") && grounded == true) {
		GetComponent<Rigidbody>().AddForce(Vector3.up*jumpPower);
		grounded = false;
		print ("Jump");
	}
}

void OnCollisionEnter ()
{
	grounded = true;
}

void OnCollisionExit()
{
	grounded = false;
}

}

Perhaps something like:

private float groundedClock = 0f;

void Update() {
  if (grounded) {
    groundedClock = 0f;  // We're grounded, don't need to time, reset timer
  } else {
    groundedClock += Time.deltaTime;  // count how long grounded has been false
  }

  if (groundedClock > 2) {  // Has grounded been false for 2 or more seconds?
    grounded = true;  // Yup, so force it true
  }
}

HTH
(Please excuse typo’s I’m not near MonoDevelop to check I haven’t fat fingered anything :slight_smile: