Moving a single object from a scene of multiple objects using keyboard controls

I have added 3 objects to my scene: a cube, a cuboid and a pyramid(cuboid is child of cube,but pyramid is not.). I want to move the pyramid on clicking the keyboard arrows.But all the 3 objects are moving,instead of pyramid alone. What could be the reason?
code snippet:

    pyramid.name='fov';
    scene.add(pyramid);
    /*some codes in between*/
    function movement(e){

          var distance = 0.15;

          var fov = scene.getObjectByName('fov');

          if(e.keyCode =='37'){

              fov.position.x += distance;

              }

          else if (e.keyCode == '38'){

              fov.position.z += distance;

          }

          else if (e.keyCode == '39'){

            fov.position.x += distance;

          }

          else if (e.keyCode == '40'){

            fov.position.z += distance;

          }

Please demonstrate your issue with a live example: https://jsfiddle.net/f2Lommf5/

It’s currently not possible to see from your code and screenshot what’s going wrong.

Here is the link to live example: https://jsfiddle.net/Athira_Narayan/w3ax6dku/10/
I want to move the pyramid along the top horizontal edge of the red cube.

Updated fiddle: https://jsfiddle.net/pgf0sdqn/

  • You have not added your event listener to the keydown event. So this line was missing:
document.addEventListener( 'keydown', onKeyDown );
  • You also have to disable the key input for OrbitControls like so:
controls.enableKeys = false;

Otherwise you translate the camera when you actually only want to translate the pyramid.

Okay. Noted that. Thankyou Mugen :slight_smile: