Using key to trigger an action

I’m trying to make it so that pressing a key, while within a certain distance attaches an geometry to the rootNode.



heres my code:



boolean activate = false;

Vector3f pos1 = cam.getLocation();

Vector3f pos2 = box1.getLocalTranslation();

float distance = pos1.distance(pos2);



if (buttonPress && distance > 30) {

activate = true;

}

if (distance > 30 && activate) {

rootNode.attachChild(boxGeo);

}





the problem is that even after activate is true when within the distance, nothing happens. the boxGeo doesnt get attached to the rootNode. How would i fix this? I appreciate all of your help.

are you sure you mean [java]distance > 30[/java] and not [java]distance < 30[/java]?^^ I just tested your code and it works, but I don’t get why you check the distance twice…

yea distance < 30, but mine still doesnt work. its seems like the program isnt registering my key stroke when i press E, every other key works.

Hi dude! Post the whole code so we can check it! ok?