Avoid Enemy Ai walk into higher ground terrain or walk on Air

How can i avoid my enemy ai to not walking into the high ground in my terrain, and at the same time i can prevent it not to walk on the air after it move to a higher ground, which i mean it moves based on the terrain’s ground height so that it will not walking on the air when it moves to a lower ground…

which language? javascript or c#?
in javascript id check if the enemy is grounded or not. if it isnt grounded add force to make it “fall” til it hits ground.

in javascript something like this…

var isGrounded : boolean = false;  //means it is not on the ground


function Update(){
    if(!isGrounded){
        rigidbody.AddForce(Vector3.Up * insert a negative number here like -1, -2, -3, etc);
    }
}

function OnCollisionEnter(col : Collision){
    if(col.gameObject.FindWithTag("Terrain")){
        isGrounded = true;
    }
}

for this to work youd need a tag on your terrain object called “Terrain” and a rigidbody on the enemy, or you can use transform.Translate instead of rigidbodys. it depends how your monster moves. force, or translations. you will probably have to edit it to fit your scripts a little. If you do it right, as long as the monster isnt standing on your terrain it will push it down if your force is high enough.(should be more than the force used to make the enemy “walk”. Hope this helped.

thank you for this script. My enemy object is use character controller actually… i have rigidbody as well… but anyhow i will try ur script 1st.

Thanks alot.