I Need help with first Person Camera Movement

Godot Version 4

Question

I Was using a toturial from 2 years ago, I had a feeling some of the code would be out of date. I had some problems but I fixed them, the following code has some errors.

	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

if Input.get_mouse_mode() = Input.MOUSE_MODE_CAPTURED:
func _unhandled_input(event: InputEvent) → void:

And The Errors are:

Line 13:Unindent doesn’t match the previous indentation level.
Line 13:Used space character for indentation instead of tab as used before in the file.
Line 14:Unexpected “if” in class body.
Line 14:Unexpected “Indent” in class body.
Line 15:Unindent doesn’t match the previous indentation level.
Line 15:Expected indented block after function declaration.
Line 15:Function “_unhandled_input” has the same name as a previously declared function.
Line 15:Expected end of file.

There is a lot missing there. Can you show the whole code and format it properly using the </> icon?

no. it’s:

DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_VISIBLE)

I don’t know how to or why you would want to get the mode. you are supposed to keep a variable that toggles between captured and visible.


if event.is_action_released("ui_cancel"):
	cursor_mode = not cursor_mode
	if cursor_mode:
		DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_CAPTURED)
	else:
		DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_VISIBLE)

pay attention to your indentations, this isn’t C# where you can skip brackets like a barbarian

tabs and spaces are not the same.
don’t copypaste code. type it in editor.

these will appear red, you don’t need us for that. just fix the issues.
and next time type the code instead of copypasting.

this is declared twice.
you can only have one of each function.

file was not saved in the correct format.

I recommed you watch the StayAtHomeGaming FPS tutorial on youtube, it’s very good and up to date.
https://www.youtube.com/playlist?list=PLEHvj4yeNfeF6s-UVs5Zx5TfNYmeCiYwf

1 Like

Thank you so much, I will make sure to make use of the toturial and your advice

My Entirety of code is the following:

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"):
		DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_VISIBLE)
 if Input.get_mouse_mode() = Input.MOUSE_MODE_CAPTURED:
	func _unhandled_input(event: InputEvent) -> void:
		if event is InputEventMouseMotion:
		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(-30), deg_to_rad(60))
if event.is_action_released("ui_cancel"):
	cursor_mode = not cursor_mode
	if cursor_mode:
		DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_CAPTURED	)
	else:
		DisplayServer.mouse_set_moode(DisplayServer.MOUSE_MODE_VISIBLE)


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("left", "right", "forward", "back")
	var direction = (neck.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	move_and_slide()

The Following is highlighted red

	DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_VISIBLE)

if Input.get_mouse_mode() = Input.MOUSE_MODE_CAPTURED:
func _unhandled_input(event: InputEvent) → void:
if event is InputEventMouseMotion:


if event.is_action_released(“ui_cancel”):
cursor_mode = not cursor_mode
if cursor_mode:
DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_CAPTURED )

Input.mouse_mode is a valid way to change the mouse behaviour.


Your indentation is crazy. Did you copy and paste this described tutorial? Godot cannot have functions defined within functions like that, and you cannot declare func _unhandled_input twice.

Here’s what I would recommend as changes

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	elif event.is_action_pressed("ui_cancel"):
		Input.mouse_mode = Input.MOUSE_MODE_VISIBLE

	# same _unhandled_input
	elif event is InputEventMouseMotion:
		# indent rotation code to be part of "mouse motion" events
		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(-30), deg_to_rad(60))

	# "ui_cancel" already handled, delete this portion 
	#if event.is_action_released("ui_cancel"):
		#cursor_mode = not cursor_mode
		#if cursor_mode:
			#DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_CAPTURED	)
		#else:
			#DisplayServer.mouse_set_moode(DisplayServer.MOUSE_MODE_VISIBLE)

And to study up on GDScript fundamentals