How to make enemy follow the player

I am trying JME and managed to load model and move model to the direction of camera in first person controller. I have poor vector math, but need to (chase the player) rotate or move enemy model as camera (player) moves through the world.



I am using following class for model moving to which I am passing cam.getDirection() vector. I am want enemy to follow the player as an when player moves in any direction.



   class ModelAnimator extends Controller {
      Node model;

      float velocity = 0.001f;
      float max = 1.20f;
      float turnSpeed = 0.001f;

                Vector3f direction;

      ModelAnimator(Node model, Float distance, Vector3f direction) {
         this.model = model;
         //reverse the direction to follow camera
         this.direction = direction.negate();
         this.direction.normalizeLocal();
      }


               public void update(float time) {
                           /******************************************************
            * Determine the location of the camera and then move the enemy.
            */
         Vector3f modelPos = model.getLocalTranslation();
         modelPos.addLocal(direction.mult(velocity));
         model.setLocalTranslation(modelPos);
      }
   }

this might help a bit:



// velocity
float vel = 150f;
// distance
float dist = enemy.distance(player);
if(dist > 1.0f) {
    // look at player (auto-rotate)
    enemy.lookAt(player, Vector3f.UNIT_Y);
    // get direction
    Vector3f dir = enemy.subtract(player).normalize();
    // move towards player
    enemy.addLocal(dir.x*tpf*vel, 0, dir.z*tpf*vel);
}



although it doesn't have the smooth rotation there.

More than the direction, you need the position. Node has a utility method called lookAt which would help you orient your enemy towards the player, then you should simply update its position a little bit in the direction of the camera position. Say:


Vector3f fromEnemy = new Vector3f( camera.getLocation() ).subtractLocal( enemy.getWorldTranslation() ).normalizeLocal().scale( time );
enemy.lookAt( camera.getLocation(), Vector3f.UNIT_Y );



You beat me  :D