Hello, im trying to make an NPC follow the character around, as it is now if the player is closer than 3f to the npc it turns to face the player.
But when the player moves further than 3f away it starts moving in the player direction (this is good) but it doesnt rotate or stop when it gets close to the player again.
It’s probably a simple solution that im missing but its gnawing at me badly.
this is the code as is in the custom control for NPC:
protected void controlUpdate(float tpf) {
Vector3f enemyPos = enemy.getWorldTranslation();
Vector3f playerPos = player.model.getWorldTranslation();
Vector3f modelForwardDir = enemy.model.getWorldRotation().mult(Vector3f.UNIT_Z);
walkDirection.set(0,0,0);
float d = playerPos.distance(enemyPos);
enemy.control.setViewDirection(playerPos);
if (d > 3f) {
enemy.control.setViewDirection(playerPos);
walkDirection.addLocal(modelForwardDir.mult(speed * tpf));
enemy.control.setWalkDirection(walkDirection);
}
}
thanks for any replies