- Home /
Enemy controller.move script slows down and speeds up. How to get it consistent?
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;
}
}
}
Hey! Did you ever get this figured out? I am having the exact same issue right now with an enemy that chases the player
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Enemy following Player on uneven surface 1 Answer
AI controller script, Enemy movement issues 0 Answers
A question about AI 0 Answers
Unity AI/Enemy won't move towards target 0 Answers