# FPS AI

Hi,

Im suing the AI code from the FPS tutorial but am having problems with it, been looking at it for a while and have simplified it to what I need. The problem is, it gets to the first waypoint and then doesnt go to the second. After tests I figured that

if (direction.magnitude < 0.5) {

return;
}

Does not return probably. This seems to be the problem that the

if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance){

curWayPoint = PickNextWaypoint (curWayPoint);
}

does not get run. Full codes below of both functions, any help would be great! Ive also checked my waypoints script and they are all linked and fine.

``````function Patrol () {

var curWayPoint = AutoWayPoint.FindClosest(transform.position);
while (true) {
var waypointPosition = curWayPoint.transform.position;

if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance){

curWayPoint = PickNextWaypoint (curWayPoint);
}

MoveTowards(waypointPosition);

yield;

}
}
``````
``````function MoveTowards (position : Vector3) {

var direction = position - transform.position;
direction.y = 0;

if (direction.magnitude < 0.5) {

return;
}

// Rotate towards the target
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);

// Modify speed so we slow down when we are not facing the target
var forward = transform.TransformDirection(Vector3.forward);
var speedModifier = Vector3.Dot(forward, direction.normalized);
speedModifier = Mathf.Clamp01(speedModifier);

// Move the character
direction = forward * speed * speedModifier;
myController.SimpleMove(direction);

SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver);
}
``````

Without looking at your code, is your AI character circling around the waypoint (just turning on the spot)? If so, try moving the waypoint marker up and down slightly and run it again (takes some trial and error).

Just a suggestion.

One problem that I had is that the default FPS waypoint scripts donâ€™t allow Y axis movement, so you need to make sure your character can reach the waypoints. If on a flat surface, make sure the waypoints are about 1 meter or so from the ground (assuming your character is about 2 meters/units tall). If you characters can move on the Y axis, you need to modify the scripts so your characters can rotate on all axis, and not only in the Y axis, so they can move up and down to reach the waypoints.