Getting enemy to move towards player untill they collide problem...

So here is the problem I want the enemy to move towards the player until there is a collision here is the important part of my enemy script:

  function Update () {
     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}

Now everything is setup to detect the collision and react properly but with the code above the enemy and player NEVER collide the enemy gets close to the player and starts to spin around the player without ever colliding…

Is the code above causing the problem?

Any solutions?

The player can completely stop moving the enemy will move forward towards the player and within a certain distance the enemy will start to orbit around the player instead of colliding with the player …

You don’t have a catch for collision in that code. So it will do that no matter what, even if you collide. Put a Box Collider around the enemy and set it to IsTrigger. Check OnTriggerEnter (other : Collider) to see if that other object is the player. If it is, set your speed to 0 (assuming you want them to stop moving).

Another option is to test their distance from each other and if their distance is less than a number you choose, you stop the NPC.