Help limiting distance on this waypoint script

Hi All

Im working on an Ai for a university project. Im not a coder by choice but facing the steep learning curve as only one other in my team will code. Im pretty new to this and admittedly biting off more than i can chew in the time i have but I dont have much choice…but boy am i learning! My comments may demonstrate only a basic understanding of the code but if anyone can help me tidy up your comments are welcome.

I have pasted a waypoint script below, basically the enemy patrols the path of waypoints unless the player gets within a certain distance in which case the enemy chases after the player. It works fine but when the enemy catches up with the player, it either jumps on top of the player, or gets underneath and pushes them both up in the air.

Im not sure if this is gravity, or a matter of limiting how close the enemy can get to the player.

Either way im not entirely sure where to implement this and hoping someone can help me out?

Thanks in advance if you can

This is my waypoint script, using tuts from @steamisM50

Have your player in the scene, mines rigidbody but any should work. Place objects in scene, Waypoint1, Waypoint 2

Add this script to enemy and set var size to 2, add player to player slot, Add waypoint1 to waypoint slot, Add waypoint2 to waypoint var

//WayPointController.js

//creates waypoint array

var waypoint : Transform;

//enemy move speed

var speed : float = 20;

//creates a number to reference waypoints

private var currentWaypoint : int;

//loop on/off
var loop : boolean = true;

//var for our target player
var player : Transform;

//vars to control detection distance
var detectPlayerDist : float = 30;

//vars to control callBack to waypoint enemy was headed to.
var callBackDist : float = 50;

//var to hold a value to keep a distance away from player //when we can wo
var keepBack : float = 5;

function Update ()
{

if (currentWaypoint < waypoint.Length)
{
		//var to hold the position of current waypoint in array
		var target : Vector3 = waypoint[currentWaypoint].position;
		//var to hold the distance between enemy and waypoint
		var moveDirection : Vector3 = target - transform.position;
		//var to hold the distance between enemy and player
		var distFromPlayer : Vector3 = player.position - transform.position;
		//var to hold a move velocity
		var velocity = rigidbody.velocity;
		//if enemy is not close to an array already, adds waypoint to the array for him to find
		if(moveDirection.magnitude < 1)
			{
				currentWaypoint++;
			}
			//else if the player is in range
		else if (distFromPlayer.magnitude < detectPlayerDist)
			{	
				//velocity direction
				velocity = Vector3.zero;
				//sets target to player
				target = player.position;
				//increases force of velocity by our player distance, multiplied by our move speed
				velocity = (player.position - transform.position).normalized * speed;
				//if the distance between player position and the next way point is less than the call back distance
				if((player.position - waypoint[currentWaypoint].position).magnitude > callBackDist)
				{
					//sets target as last waypoint target headed for
					target = waypoint[currentWaypoint].position;
					//sets velocity force
					velocity = moveDirection.normalized * speed;
				}
			}
		else
			{	
				//keep moving inn that direction
				velocity = moveDirection.normalized * speed;
			}
}
else
{
	//if loop is on
	if(loop)
		{
			//then set the current waypoint array back to 0 to start aagain
			currentWaypoint = 0;
		}
	else
		{
			//if not stop at last waypoint in list
			velocity = Vector3.zero;
		}
}
//move accortding to velocity value
rigidbody.velocity = velocity;
//look at the target
transform.LookAt(target);

}

why it is that my AI cant detect my player or target?

You need I drag and drop the target objects to the script in the hierarchy panel

ok now,. is see the problem.
edit this code

if((player.position - waypoint[currentWaypoint].position).magnitude > callBackDist)
{
//sets target as last waypoint target headed for
target = waypoint[currentWaypoint].position;
//sets velocity force
velocity = moveDirection.normalized * speed;
}

change this to

if(player.position.magnitude - waypoint[currentWaypoint].position.magnitude > callBackDist)

      {

          //sets target as last waypoint target headed for

          target = waypoint[currentWaypoint].position;

          //sets velocity force

          velocity = moveDirection.normalized * speed;

      }