Best way to keep an ai grounded?

Hi so right now I have a basic script that patrols waypoints and follows and attacks a player when it gets near. I have a script that tries to keep it grounded but it is buggy and doesn’t work all the time. I was wondering what’s the best way to keep the ai on the ground.

var waypoint : Transform[]; // The amount of Waypoint you want
var patrolSpeed : float = 3;       // The walking speed between Waypoints
var loop : boolean = true;       // Do you want to keep repeating the Waypoints
var player : Transform;          // Referance to the Player
var dampingLook = 6.0;          // How slowly to turn
var pauseDuration : float = 0;   // How long to pause at a Waypoint
var attackRange = 10;          // Range to start the attack
var attackSpeed = 5.0;          // Speed to attack
var attackLook = 10.0;          // How fast to turn when attacking
var ASpeed = 1;

private var distanceToPlayer : int; // Distance from the enemy to the Player
private var currentTime : float;
private var currentWaypoint : int = 0;
private var character : CharacterController;
private var gravity : float = 2.0;
private var attacking : boolean = false;
private var isattacking : boolean = false;
private var captured : boolean = false;

    function Awake(){

    }
    
    function Start(){
        character = GetComponent(CharacterController);
        animation["bite"].speed = ASpeed;
        animation["bite"].layer = 1;
    }
    
    function Update(){
    	// Gets the distance between the enemy and the Player	
    	distanceToPlayer = Vector3.Distance(player.position, transform.position);
    	if(captured){
    	     animation["Death"].wrapMode = WrapMode.ClampForever;
             animation.Play("Death");
       	}
	    else if(distanceToPlayer < attackRange){
	       attacking = true;
	       attack();
	       }else{
	       attacking = false;
	       }
		    if(currentWaypoint < waypoint.length && !attacking && !captured){
		       patrol();
	       	}else{    
			    if(loop && !attacking){
			       currentWaypoint=0;
				} 
	    	}
	}
    function patrol(){
        var target : Vector3 = waypoint[currentWaypoint].position;
        target.y = transform.position.y; // This keeps the waypoint at character's height
        var moveDirection : Vector3 = target - transform.position;
        if(moveDirection.magnitude < 0.5){
           if (currentTime == 0) {
             currentTime = Time.time; // Pause over the Waypoint
             animation.Play("idle");
           }
           if ((Time.time - currentTime) >= pauseDuration){
             currentWaypoint++;
             currentTime = 0;
           }
        }else{        
    		// This gets the location of the waypoint and rotates the AI to look at the waypoint and dampen the rotation
           var rotation = Quaternion.LookRotation(target - transform.position);
           transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
		   moveDirection.y = 0;
	       moveDirection.y -= gravity; //Gravity so the the AI  stays on the ground
           character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
           animation.Play("trot");
        }  
    }
 
    function attack(){
	    // Attack the Player
	    if (distanceToPlayer <= 2) {
	    	animation.Play("bite");
	    } else {
	    animation.Stop("bite");
	    // Rotate to face the Player
	    var lookPos = player.position - transform.position;
	        lookPos.y = 0;
	    var rotation = Quaternion.LookRotation(lookPos);
	        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * attackLook);
	    // Move towards the Player
        var target : Vector3 = player.position;
        var moveDirection : Vector3 = target - transform.position;
        moveDirection.y = 0; // This helps stop the AI running up off the floor to match the Players Y axis
        moveDirection.y -= gravity; //Gravity so the the AI always stays on the ground
        character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);
        animation.Play("run");
    	}
    }
    
    function OnCollisionEnter(collision : Collision){
    	if(collision.gameObject.tag == "Projectile"){
    		captured = true;
    	}
    }

function OnCollisionEnter(collision : Playa){
if(playa.gameObject.tag == “ground”){ //tag ground
isGrounded = true; //new bool
}
else
{
isGrounded = false;
}
}
function Update()
{
if(isGrounded == false)
{
transform.Translate = Vector3(0,-1,0)*Time.deltaTime;
}
}

Should work alright, pseudo code so dont copy pasta, but something along these line should work