Godot Version
4.3
Question
` I am trying to make a game that can be played both using a keyboard/mouse and a controller. It is a first person game, and all of the code works except for the camera controls in joypad.
I have followed many tutorials and none of them seem to work. I’ve checked my input mapping on the project settings and i have configured the inputs correctly (right joystick directions in this case). It feels like it just refuses to register the joystick movement. `
CODE BELOW:
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
elif event.is_action_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
neck.rotate_y(-event.relative.x * 0.01)
camera.rotate_x(-event.relative.y * 0.01)
#clamp vertical
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-CAMERA_CLAMP_DEGREES), deg_to_rad(CAMERA_CLAMP_DEGREES))
elif event is InputEventJoypadMotion:
var joystick_camera_input := Input.get_vector("turn_left", "turn_right", "turn_up", "turn_down")
if joystick_camera_input.length() >= 0.2:
neck.rotate_y(deg_to_rad(-joystick_camera_input.x * 1.5))
camera.rotate_x(deg_to_rad(-joystick_camera_input.y * 1.5))
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-CAMERA_CLAMP_DEGREES), deg_to_rad(CAMERA_CLAMP_DEGREES))
Hello!
I would advice that you implement the keyboard+mouse controller and the joypad controller separately so that you don’t have to deal with confusing if-else
.
In the code you wrote, it seems that you first need to use the mouse before you can use the controller. Furthermore, the joypad is only going to affect the player if the mouse is not sending any movement signals.
Maybe try something like:
enum CONTROLLER_TYPES {JOYPAD, MOUSE_KEYBOARD}
@export var controller_type: CONTROLLER_TYPES
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if controller_type == CONTROLLER_TYPES.JOYPAD:
handle_joypad_inputs(event)
elif controller_type == CONTROLLER_TYPES.MOUSE_KEYBOARD:
handle_mouse_keyboard_inputs(event)
func handle_joypad_inputs(event: InputEvent):
if event is InputEventJoypadMotion:
var joystick_camera_input := Input.get_vector("turn_left", "turn_right", "turn_up", "turn_down")
if joystick_camera_input.length() >= 0.2:
neck.rotate_y(deg_to_rad(-joystick_camera_input.x * 1.5))
camera.rotate_x(deg_to_rad(-joystick_camera_input.y * 1.5))
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-CAMERA_CLAMP_DEGREES), deg_to_rad(CAMERA_CLAMP_DEGREES))
func handle_mouse_keyboard_inputs(event: InputEvent):
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
neck.rotate_y(-event.relative.x * 0.01)
camera.rotate_x(-event.relative.y * 0.01)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-CAMERA_CLAMP_DEGREES), deg_to_rad(CAMERA_CLAMP_DEGREES))