i am trying to make a simple AI script so my zombie will follow the character slowely at first and then faster as it gets closer and finally attack when it is up close. the script i made is:
var speed : int = 5;
var Wspeed : int = 1;
var gravity : int = 9;
var moveDirection : Vector3 = Vector3.zero;
var player : Transform;
var Wchase : int = 20;
var chase : int = 10;
var Dchase : int = 2;
var timer : float = 5.0;
function Update(){
var range = Vector3.Distance(player.position, transform.position); //calculate distance between player and enemy
print(range);
var controller : CharacterController = GetComponent(CharacterController);
if(Wchase > range && range > chase)
{
transform.LookAt(player);
moveDirection = Vector3(0, 0, Wspeed);
moveDirection = transform.TransformDirection(moveDirection) * Wspeed;
print("walk");
}
if(chase > range && range > Dchase)
{
transform.LookAt(player);
moveDirection = Vector3(0, 0, speed);
moveDirection = transform.TransformDirection(moveDirection) * speed;
print("run");
}
if(Dchase > range)
{
transform.LookAt(player);
moveDirection = Vector3.zero; //set movement to 0
attack(); //call the attack function
print("attack");
}
moveDirection = Vector3(0, -gravity, 0);
controller.Move(moveDirection * Time.deltaTime); //move enemy
}
function attack(){
timer -= 1 * Time.deltaTime; //attack speed
if(timer <= 0)
{
timer = 1.0;
player.GetComponent("player_health").health -= 10; //take health away from player
}
}
when i take out the moveDirection = Vector3(0, -gravity, 0); part it works but has no gravity obviously. with the gravity part added it looks at the player but does not move. why is this? i realise that this moves it continuously down but why does it not move any other way?