Having Trouble with my gun

Hello Guys so ive been learning Jmonkey for a while and just practicing with the tutorials.So what ive done is added an Ak-47 to the FPS cam.the gun is good,

but i have two problems

  1. i used the following code to move the gun with the player


    if (down)

    { walkDirection.addLocal(camDir.negate());

    Weapon.setLocalTranslation(cam.getLocation().x+1.5f, cam.getLocation().y-2,cam.getLocation().z);



    it is working but it causes flickering of gun while i move.

    2)when i move my mouse (i.e) turn the player the gun is not turning it still remains in the same axis.Please help with these also if there is a better way to stick the gun with the player please tell.

The way you try to put your weapon off-center doesn’t work because you just move it +x and -y in world coordinates:


You’ll also have to learn some basic vector math to not constantly run into these kinds of problems:


Basically you have to add a direction vector from the character which you can create like Vector3f forward = character.getWorldRotation().mult(Vector3f.UNIT_Z));