#FPS Character controls

1 messages · Page 1 of 1 (latest)

winter merlin
#

I have done so much research and practice and i cant figure out why when i set up FPS camera movements it just never has vertical movement. I even followed super specific instructions and nothing. when my mouse makes vertical movement it tilts the camera instead. But Horizontal movements are fine.

soft sequoia
#

Not an extreme 1st-person expert, but I do have a project where I implemented movement and camera controls. Note that my camera node is a child of the player's character body node.

For applying camera controls, I do the following:

  • Horizontal rotation: This applies directly to the character body. If my player wants to rotate, the character itself should rotate to face the new direction. If I only applied the rotation to the camera, the body wouldn't rotate to face the new direction. With the camera being nested under the body, the camera also effectively rotates since it's parent does. Because I apply this to the whole character body node, I rotate the node on the Y axis using rotate_y(rotation_amount_here).
  • Vertical rotation: This applies directly to the camera node. I don't want the whole body to rotate with this one (would look like front flips/back flips), but if you wanted to do something like rotate a part of the model skeleton (eg. head) you could set that up as well (more involved than what I'm needing). Because I only need to rotate the camera, I rotate the camera on the X axis using rotate_x(rotation_amount_here).

And I do this for input management (PS. this is arguably overkill and likely has a better way to handle this):

  • Controller inputs: I have these handled in the _physics_process() method. If the player is inputting a consistent direction to immediately turn the camera on-load (eg. on respawn or in loading a new level), these inputs will always get caught. If I did this in something like _unhandled_input(), the camera input would only register after the joystick input changed which doesn't feel very good to me.
  • Mouse inputs: These I do handle in _unhandled_input() since any mouse movement transfers to a single rotation input. Also I had issues implementing these alongside the controller inputs (for what reason I don't exactly recall right now).
#

Attached is a screenshot of the player character body with the Camera node set up as a child of the body node. And this is the highest level code snippet in my code that does the final rotation logic (after other code retrieves and processes the inputs)

func _apply_look_rotation(horizontal_rotation: float, vertical_rotation: float):
  rotate_y(horizontal_rotation)
  camera.rotate_x(vertical_rotation)
    
  rotation.y = fmod(rotation.y, 2 * PI) # Don't let the rotation go up/down forever and hit a number storage boundary
  camera.rotation.x = clamp(camera.rotation.x, MIN_CAMERA_DOWN, MAX_CAMERA_UP) # Prevent looking past straight up or down