Make camera rotation reversed when upside down

Godot Version

Godot 4.2

Question

So in my game when you are in the air the camera can be turned upside down, when the camera is upside down moving the camera left and right is reversed, I am trying to make it so the camera behaves as expected when upside down
This is what I have written:

func _unhandled_input(event):
	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 !(camera.rotation.x < -1.5 or camera.rotation.x > 1.5):
			neck.rotate_y(-event.relative.x * 0.003)
		elif event is InputEventMouseMotion:
			neck.rotate_y(-event.relative.x * -0.003)
		camera.rotate_x(-event.relative.y * 0.002)

This is the error I get:

Invalid get index 'relative' (on base:'InputEventMouseButton').

If anyone could help me figure this out that would be amazing! thank you!

  • indent the “camera.rotate_x(-event.relative.y * 0.002)” and that should fix the error
  • for the upside-down inverted camera, if flip both the character and the camera at the same time it should work as intended
1 Like

This worked! I completely forgot about indentation there, thank you so much!