i want to make enemy units in my game be affected by gravity, so that when they walk off a ledge they fall down. ive tried to use c_trace to check whether or not the ground is under the units which will increase a local variable that makes the units fall down to the ground faster and faster until they hit the ground where the value is set to 0.

the problem is that half the time they dont react at all after walking off a ledge and other times they slam into the ground and other times they fall down as they should but it is not consistent at all.

Code:
action enemy()
{
	var grav;
	while(my.health > 0)
	{
		if (c_trace(my.x, vector(my.x, my.y, my.z - 15), IGNORE_ME) == 0)
		{
                        grav = grav + 1 * time_step;
			c_move(me, nullvector, vector(0, 0, - grav * time_step), GLIDE);
		}
                if (c_trace(my.x, vector(my.x, my.y, my.z - 15), IGNORE_ME) != 0)
                {
                        grav = 0;
                }
		wait(1);
	}
	ent_remove(me);
}



have i done something completely wrong or is there a better method of doing it?