Enemy AI gravity and animation

I have been trying to make an enemy and-through youtube tutorials and unity answers- have gotten about half of it to work(chase player, turn towards player and attack) but have had a problem with the gravity and animation. When the enemy moves towards the player it ignores gravity and fly’s around and goes through anything in its way, unless if there is a RigidBody attached to it(if kinamatic is on it goes through the colliders again). I got the code from youtube

var Distance;
var Target : Transform;
var LookAtDistance = 35.0;
var ChaseRange = 25.0;
var AttackRange = 2.0;
var MoveSpeed = 5.0;
var Damping = 4.0;
var AttackRepeatTime = 1;

var Damage = 10;

private var AttackTime : float;

var controller : CharacterController;

//var gravity : float = 20.0;

private var MoveDirection : Vector3 = Vector3.zero;

function start(){
AttackTime =  Time.time;
}

function Update(){

if(transform.rotation.x!=0){
transform.rotation.x=0;
}if(transform.rotation.z!=0){
transform.rotation.z=0;
}
Distance = Vector3.Distance(Target.position, transform.position);


if (Distance < LookAtDistance){

LookAt();
}

if(Distance > LookAtDistance){
//renderer.material.color = Color.green;

}
if(Distance < AttackRange){
Attack();

}else if(Distance < ChaseRange){

Chase();

}

}

function LookAt(){
//renderer.material.color = Color.yellow;

var rotation = Quaternion.LookRotation(Target.position - transform.localPosition);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);


}

function Chase(){
//	renderer.material.color = Color.red;
	
	moveDirection = transform.forward;
	moveDirection *= MoveSpeed;
	
//	moveDirection.y -= gravity * Time.deltaTime;

	controller.Move(moveDirection * Time.deltaTime);
Physics.gravity=Vector3(0,-20,0);
}

function Attack(){


if(Time.time > AttackTime){
Target.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.Log("10 Damage");
AttackTime = Time.time + AttackRepeatTime;
}

}
function ApplyDamage(){
ChaseRange+= 30;
MoveSpeed +=2;
LookAtDistance += 40;
}

Could someone please tell me what is wrong with it?

Also animations seem to not want to work with it and make it the enemy go slower and start jittering around, but that isn’t my main concern at this point.

thank you for any help you can give.

Physics.Gravity will only affect non-kinematic rigidbodies. If you want gravity on something other than a rigidbody, you have to simulate it yourself.

Unity’s documentation has good examples of how to add gravity to a CharacterController. This should help.

Move simply moves the character in the desired direction, thus you must apply gravity yourself. Usually it’s easier to control enemies with SimpleMove, which applies gravity automatically. Different from Move, SimpleMove expects the velocity vector instead of a displacement, which make things even easier:

function Chase(){
    moveDirection = transform.forward * MoveSpeed;
    controller.SimpleMove(moveDirection);
}

The only problem with SimpleMove is that it takes absolute control of the Y axis, thus the enemy can’t fly or jump (SimpleMove ignores Y in moveDirection).