How to set a Gun in a Fps Game

HI help required here

i tried to move the spatial
problem is that camera has a getDirection() method but a spatial doesnt have it !!
So even if we try to Translate it it wil just change its postiton
but the direction will not change:

P.S please forgive my English

Camera also has getRotation, you can get its rotation with that method and apply it to spatial with setLocalRotation.
N.B. Camera.getRotation returns the rotation relative to world while Spatial.setLocalRotation sets the rotation relative to the spatial parent, so you may need to convert it depending of the structure of your scene.

Can’t we use Look at
AND yes HOW To convert this value

Oh, yes you can absolutely use lookAt. I totally forgot this method.

For the conversion, assuming that you have c_wr that is your camera rotation and sp that is your spatial, you have to get the world rotation of sp.getParent() then you invert it and multiply for c_wr the resulting quaternion should be the local rotation that you have to apply to sp.
Not 100% sure about this though. Quaternions are still quite a mystery to me.

what do u mean by invert???

This is what I do


In a classic FPS you can set your weapon writing this:


Than experiment with various adjustments as it all depends on movement and models you use for weapons and player.


What About the Location @Xuan Problem is that as soon as i move my head towards other direction then the gun stays at its point
i think this is it BUT with a a bug

Depends on how you control your character. I use the BetterCharacterControl, so I need to update the camera position with the character’s world translation. But if your inputs affect the camera directly you’ll need to update the weapon’s node translation with the camera position (see aegroto’s post).

The thread you linked explains how to avoid having the gun disappear when you get too close to a wall, I recommend you to tackle the basic stuff first.

thanks to all i understand that there is abug in that but still thanks to all !!! ;

There is no bug.

A bug in what?

in that Code which was given by that person it was actually that the gun went inside the walls

My suggestion: Don’t control the camera directly (set location, rotation,…),
But use a CameraNode (or a Node with CameraControl) :

  • attach you gun (spatial) as chlid of CameraNode
  • link CameraNode to your camera (iirc .setCamera(…))
  • input drive your CameraNode

Then the gun will follow the cameraNode, like the Camera, and you’ll be able to add local animation to the gun easily (walk, reload, …)

javadoc : CameraNode

i appreciate that but can you be more specific!!

more specific about what ?

CameraNode allow you to create a Node that drive camera or driven by camera. CameraNode is like having eyes node in your FPS.

thanks for all the help i appreciate it