So i have been working on a AI like script, with the enemy walking around with waypoints. And if the player walks close enough to the enemy a raycast will notice the player and start chasing it. The enemy will also walk back the the current waypoint if the enemy gets to far away from the waypoint.
But the problem i’m having is that the enemy stops moving from time to time when walking between waypoints. And the only way to make him move again is to move into the raycast and run out of it so he starts walking to the current waypoint again.
I have no idea why this is happening so if anyone have any ideas what might solve this i would be very happy, here is the script by the way.
// Waypoint Variables
var waypoint : Transform[];
var waypointSpeed : float = 20.0;
var Speed : float = 5.0;
private var currentWaypoint : int;
var loop : boolean = true;
var Player : Transform;
// Raycasting Variables
var playerObject : GameObject;
var rayRange : float;
private var rayDirection = Vector3.zero;
function Update ()
{
// Raycasting
var distance = Vector3.Distance(playerObject.transform.position, transform.position);
var hit : RaycastHit;
rayDirection = playerObject.transform.position - transform.position;
if(currentWaypoint < waypoint.length)
{
// Waypoint
var Target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = Target - transform.position;
var distFromPlayer : Vector3 = Player.position - transform.position;
var velocity = rigidbody.velocity;
// If enemy gets close enough to current waypoint switch to next waypoint.
if(moveDirection.magnitude < 0.5)
{
currentWaypoint ++;
}
// Raycasting.
else if (Physics.Raycast (transform.position, rayDirection, hit, rayRange))
{
if (hit.transform.tag == ("Player"))
{
// Start chasing player.
velocity = Vector3.zero;
Target = Player.position;
velocity = (Player.position - transform.position).normalized * Speed;
// Enemy gets to far away from the current waypoint, go back to it.
if((Player.position - waypoint[currentWaypoint].position).magnitude > 20) {
Target = waypoint[currentWaypoint].position;
velocity = moveDirection.normalized * waypointSpeed * Time.deltaTime;
}
}
}
// Move between waypoints.
else
{
velocity = Vector3.zero;
Target = waypoint[currentWaypoint].position;
velocity = moveDirection.normalized * waypointSpeed * Time.deltaTime;
}
}
else
{
if(loop)
{
currentWaypoint = 0;
}
else
{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(Target);
}
Also just tested around in the game why he stopped, what i saw is that he wont move if the player is to close to him, not close enough so the enemt notice him but enough to stop the waypoint code from working.
I dont know if this answers your question but givin the problem that you had, I thought I would just outline a new script to do this. Adding in a few features that I thought would be nice. So I didnt read all of your script. 
It is basically commented where you would need to fill in some stuff. That stuff could be gotten from your script easily.
I got rid of your internal raycasting stuff, in favor of a basic IsVisible() function. If your movement (vertically) were dependent on raycasts then you could make one that says DistanceToGround() and get the hit point and distance there.
In your movement section, I would have the character move autonomously and not by math. So you set your next move position, see if he is on the ground and such, or simply use a character controller and set the direction and movement and let it do the rest.
// Waypoint Variables
var waypoint : Transform[];
//var waypointSpeed : float = 20.0;
var Speed : float = 5.0;
var WaypointSlop : float=5.0;
private var currentWaypoint : int=-1;
private var waypointDirection : int=1;
var loop : boolean = true;
//var Player : Transform;
var Enemy : Transform;
private var Target : Transform;
private var LastPostion : Vector3=Vector3.zero;
function Start(){
Target=GameObject.CreatePrimitive(PrimitiveType.Cube).transform;
Target.renderer.enabled=false;
ChooseNextWaypoint();
}
function Update ()
{
doMove();
}
function doMove(){
if(Enemy){
Target.position=Enemy.position;
//See if we are close enough to attack isVisible(Enemy)
//attack
// if we cannot move and attack at the same time, exit
}else{
if(Vector3.Distance(transform.position, Target.position) < WaypointSlop)
ChooseNextWaypoint();
}
// move towards the target at speed (running would be an option too.. ;)
// if we have an enemy and we are beyond our chase distance..
Target.postion=LastPostion;// set our move to move to the last point we were at before this attacking
}
function ChooseNextWaypoint(){
// this is basic. I would put something in that says if currentWaypoint==-1 then choose the closest point. ;)
currentWaypoint += waypointDirection;
if(currentWaypoint = waypoint.length || currentWaypoint = -1){
if(loop){
currentWaypoint=0;
waypointDirection=1;// just in case we swap it at runtime
}else{
waypointDirection=-waypointDirection;
currentWaypoint+=waypointDirection * 2;
}
}
Target.position=waypoint[currentWaypoint].position;
}
function IsVisible(object : Transform){
var hits : RaycastHit[];
var ray : Ray = new Ray (transform.position, transform.position-object.position);
hits = Physics.RaycastAll (ray, Vector3.Distance(transform.position, object.position));
for (var i = 0;i < hits.Length; i++) {
var hit : RaycastHit = hits[i];
var obj : Transform = hit.collider.transform;
// is the object anything but this or the one we are searching for
if (obj != transform obj != object) return false;
// is the object the one we are searching for?
if (obj == object) return true;
}
// this should never happen...
return true;
}
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Player" !Enemy){
if(isVisible(other.transform)){
Enemy=other.transform;
LastPostion=transform.position;
}
}
}