I have been making a game with enemies that follow and attack you when you get so close to them. I just imported a model from Blender3D when I give it a character controller and the AI script and everything it will follow and attack me, but for some reason it moves sideways. I can’t figure out why this is.
var distance;
var target : Transform;
var lookAtDistance = 15.0;
var chaseRange = 15.00;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var Controller : CharacterController;
var Gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
var AttackRepeatTime = 1;
private var AttackTime : float;
var TheDamage = 20;
function Start()
{
AttackTime = Time.time;
}
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
lookAt ();
}
if(distance < attackRange)
{
attack();
}
else
{
if(distance < chaseRange)
{
chase ();
}
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function chase ()
{
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= Gravity * Time.deltaTime;
Controller.Move(moveDirection * Time.deltaTime);
}
function attack ()
{
if(Time.time > AttackTime)
{
target.SendMessage("PlayerHit", TheDamage, SendMessageOptions.DontRequireReceiver);
AttackTime = Time.time + AttackRepeatTime;
}
}