So I have a script for an enemy to move towards the player once he is within a certain range. I'm using a character controller for this as I do not need or want any physics effects. However, sometimes the enemy will be moving normally, then start to slow down randomly. It seems like it has to do with the distance the enemy is from the player because it always slows around the same range and speeds back up when it's only a few units away from the player. Here is the code that I have:
var playerTarget : Transform;
var moveDirection = Vector3.zero;
var grounded : boolean = false;
var gravity = 32.0;
var aggroRange = 50;
var attackRange = 2;
function Start () {
}
function Update () {
var playerDistance = Vector3.Distance(playerTarget.position, transform.position);
if(playerDistance > attackRange){ //stops the movement when it is close enough to attack.
if(playerDistance <= aggroRange) //only moves when it is close enough to the player
{
//Establish rotation towards player
transform.LookAt(playerTarget);
moveDirection = transform.TransformDirection(Vector3.forward);
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
Debug.Log(moveDirection);
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * 8.0 * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
}
}
}
↧