Walking through air

You saw this code in a previous topic perhaps.

var moveDir = 1;
var hp = 1;
var dp = 1;
var texture : Texture2D;
var walk : Texture2D;
var pain : Texture2D;
var hero : Hero;
var animating = true;
var frameIndex = 1;
var lastframe = 4;
var repeatrate = 0.2;
InvokeRepeating ("Animate", 0.1, repeatrate);

function Update () {
   transform.position.z = -5.5;
   renderer.material.mainTexture = texture;
   if (moveDir == 1) {
      transform.position.x -= .01;
   }
   if (moveDir == 2) {
      transform.position.x += .01;
   }
}

function OnCollisionEnter (c : Collision) {
   //We hit the Hero! What to do?
   if (c.gameObject == hero.gameObject) {
      //If he's above us.. ow! Take damage and send the guy flying!
      //If he's below us.. yay! Hurt him and send him flying!
      if (c.transform.position.y - 0.9 > transform.position.y) {
            texture = pain;
            lastframe = 7;
            frameIndex = 0;
            moveDir = 0;
            hero.transform.position.y -= .2;
            hero.texture = hero.jleftTex;
            hero.grounded = false;
            hero.rigidbody.AddForce (0, 350, 0);
            hp -= hero.dp+1;
      } else {
         if (moveDir == 1) {
            moveDir = 2;
         } else if (moveDir == 2) {
            moveDir = 1;
         }
         hero.hp -= dp;
      }
   }
}

function OnTriggerEnter (other : Collider) {
   if (other.name == "enemyTrigger") {
      if (moveDir == 1) {
         moveDir = 2;
      } else if (moveDir == 2) {
         moveDir = 1;
      }
   }
}

function Animate () {
   frameIndex += 1;
   
   if (frameIndex == lastframe){
      if (texture == pain) {
         texture = walk;
         frameIndex = 0;
         moveDir = 1;
         lastframe = 4;
      } else {
         frameIndex = 0;
      }
   }
   
    texture.frame = frameIndex;
}

When you jump on the enemy this is supposed to set your grounded to false, which it does… sometimes. Other times it doesn’t set grounded to false, thus letting you walk through midair. Why does it only work sometimes?

Possibly moving to quickly for the check (similar to how some collision miss).

One thing I forgot to mention was that by: “which it does… sometimes” I meant during an entire play session it will work perfectly and during another it will not at all.