Can't use lookat() multiple times

i have an array created by method, when i debug everything looks fine but 1 out of 10 mob face where i want.

    private Node mob[] = new Node[number];
    private Node[] regularMob(Vector3f vec){
            for(int i = 0; i < number; i++){
                mob[i] = (Node) assetManager.loadModel("Models/Mob.j3o");
                mob[i].setName("blah"+i);
                mob[i].setLocalTranslation(vec);
                control = mob[i].getControl(AnimControl.class);
                channel = control.createChannel();
                channel.setAnim(Walking);
                
            }
            return mob;
        }
        
        public void mobZ(int number){
            for(int i = 0; i < number;i++){
                
                Vector3f vec = new Vector3f(FastMath.nextRandomInt(-100, 100),0, FastMath.nextRandomInt(-100, 100));
                rootNode.attachChild(regularMob(vec)[i]);
            }
        }
      
@Override
    public void update(float tpf){
       for (int i = 0; i < number; i++){
            mob[i].lookAt(cam.getLocation(), Vector3f.UNIT_Y);
            mob[i].rotate(0, FastMath.DEG_TO_RAD * 180, 0);
            
        }
    }

Have you really looked at your code?:

Shouldn’t this method be like this?:

    private Node regularMob(Vector3f vec) {
        Node newMob = (Node) assetManager.loadModel("Models/Mob.j3o");

        newMob.setName("blah"+i);
        newMob.setLocalTranslation(vec);

        control = newMob.getControl(AnimControl.class);
        channel = control.createChannel();
        channel.setAnim(Walking);

        return newMob;
    }

And so on, adding the mob to the array after this method?:

    public void mobZ(int number) {
        Vector3f vec = new Vector3f(FastMath.nextRandomInt(-100, 100),0, FastMath.nextRandomInt(-100, 100));
        for(int i = 0; i < number; i++) {
            Node newMob = regularMob(vec);
            mob[i] = newMob;
            rootNode.attachChild(newMob);
        }
    }

In your original code you are overriding every array item each time on each iteration. So after the last mob is created, the full array is containing that mob.

1 Like

This is what the other reply is talking about. Each pass through the loop you create the entire array again and add the i’th item.

You ultimately end up with number * number amount of mobs and only the last ā€˜number’ of them will face the way you want.

Well, my very first game so. Thank you