It looks like you’re using a lot of extra and over complicated code for aiming; I’m not sure where the error is exactly but I imagine it has to do with an improper integer value being returned by your method.
Although you could definitely simplify your code using just the built-in functions for Vector math, and eliminate the extra math so it’s easier to debug.
I would personally do something like this to make the NPC look at the nearest player. (unless there’s some other reason you need to have the look direction in the form of an int)
public Vector3f aimForNearestPlayer(List<Player> players) {
Vector3f clC = null;
float cDist = Float.MAX_VALUE;
for(Player p : players) {
float cD = p.position.distance(this.spear.getWorldTransform().getTranslation());
if (cD < cDist && p != this) {
cDist = cD;
clC = p.position;
}
}
System.out.println(clC);
if (clC == null) return this.rot; // in this case, rot is a Vector3f that represents current lookDirection rather than an int
Node playerNode = player.getNode();
playerNode .lookAt(clC, Vector3f.UNIT_Y);
rot = cLc; // store the last direction that the player looked
return rot;
}
Rather than creating a new node to extract the ViewDirection like you did in your example, I wold just apply that to the player’s node and cut out the integers.