Camera movement does not work! HELP!

Godot Version

Godot 4.4

Question

Can someone pls help me and fix this code! I tried using AI but they messed up my movement, and it also would not work to change my mouse mode while I press ESC
here is my script:

extends CharacterBody3D

@export var playerSpeeed = 5.0
@export var jumpForce = 10.0
@export var gravity = 8.0
@export var mouseSensivity = 0.5

@onready var head = $Head
@onready var camera = $Head/Camera3D

var direction = Vector3.ZERO
var camera_x_angel = 0.0

func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _Input(event):
if event is InputEventMouseMotion:
head.rotate_y(-deg_to_rad(event.relative.x * mouseSensivity))

	camera_x_angel += event.relative.y * mouseSensivity
	camera_x_angel = clamp(camera_x_angel, -90.0, 90.0)
	camera.rotation.x = -deg_to_rad(camera_x_angel)
	
elif event is InputEventKey:
	if Input.is_key_pressed(KEY_ESCAPE):
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

func _process(delta):
direction = Input.get_axis(“left”, “right”) * head.basis.x + Input.get_axis(“forward”, “backward”) * head.basis.z
velocity = direction.normalized() * playerSpeeed + velocity.y * Vector3.UP

if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y += jumpForce
else:
	velocity.y -= gravity * delta

move_and_slide()

Can you tell us what the problem is?

What do you expect to happen vs. what actually happens?

2 Likes

I want to have fps movement, and that works. but I have an camera on the node “Head” but it doesnt move around with the cursor. and i want that the camera rotates with the cursor so i can look around. :cry:

@tibaverus can you pls help. My life is ending on this problem

I wouldn’t try to vibecode a basic concept like this. Have you watched a tutorial yet? https://www.youtube.com/watch?v=A3HLeyaBCq4

The input function seems good, other than the mis-capitalized name, you may notice the function is missing it’s blue arrow by the line number; this is the correct signature:

func _input(event: InputEvent) -> void:

Your move_and_slide should only happen during physics, so change _process to _physics_process.

The direction could be massively simplified with Input.get_vector and using basis which I assume you were trying to do but the forward/right vectors don’t rotate like the basis does on it’s own.

func _physics_process(delta: float) -> void:
	var input_direction: Vector2 = Input.get_vector("left", "right", "forward", "backward")
	var direction: Vector3 = head.basis * Vector3(input_direction.x, 0, input_direction.y)

	# you should use a seperate horizontal/vertical velocity so you can preserve gravity
	var vertical_velocity := velocity.project(up_direction)
	var horizontal_velocity := velocity - vertical_velocity

	# assign player movement on a horizontal plane, not overwriting gravity
	horizontal_velocity = direction * playerSpeeed

	# gravity and jumping, using vertical_velocity
	if Input.is_action_just_pressed("jump") and is_on_floor():
		vertical_velocity.y += jumpForce
	else:
		vertical_velocity.y -= gravity * delta

	# re-combine horizontal and vertical velocity
	velocity = vertical_velocity + horizontal_velocity
	move_and_slide()

Make sure to paste code with proper formatting between three ticks ```

I think this is the right way but that I used it wrong. the camera rotation still is not working can you check my new code?

extends CharacterBody3D

@export var playerSpeeed = 5.0
@export var jumpForce = 10.0
@export var gravity = 8.0
@export var mouseSensivity = 0.5

@onready var head = $Head
@onready var camera = $Head/Camera3D

var direction = Vector3.ZERO
var camera_x_angel = 0.0

func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _Input(event: InputEvent) → void:
if event is InputEventMouseMotion:
head.rotate_y(-deg_to_rad(event.relative.x * mouseSensivity))

camera_x_angel += event.relative.y * mouseSensivity
camera_x_angel = clamp(camera_x_angel, -90.0, 90.0)
camera.rotation.x = -deg_to_rad(camera_x_angel)

func _input(event):
if event is InputEventKey:
if event.scancode == KEY_ESCAPE and event.pressed:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

func _process(delta):
direction = Input.get_axis(“left”, “right”) * head.basis.x + Input.get_axis(“forward”, “backward”) * head.basis.z
velocity = direction.normalized() * playerSpeeed + velocity.y * Vector3.UP

func _physics_process(delta: float) → void:
var input_direction: Vector2 = Input.get_vector(“left”, “right”, “forward”, “backward”)
var direction: Vector3 = head.basis * Vector3(input_direction.x, 0, input_direction.y)

# you should use a seperate horizontal/vertical velocity so you can preserve gravity
var vertical_velocity := velocity.project(up_direction)
var horizontal_velocity := velocity - vertical_velocity

# assign player movement on a horizontal plane, not overwriting gravity
horizontal_velocity = direction * playerSpeeed

# gravity and jumping, using vertical_velocity
if Input.is_action_just_pressed("jump") and is_on_floor():
	vertical_velocity.y += jumpForce
else:
	vertical_velocity.y -= gravity * delta

# re-combine horizontal and vertical velocity
velocity = vertical_velocity + horizontal_velocity
move_and_slide()

I used a tutorial for this code, but i think i did something wrong because it didnt work

you should only have one func _input, the capitalized version is not an override so it does nothing, unlike the lower case _input which is correctly named.

Thank you so much! your a life saver! but I have another problem in the same project now… I’m not forcing you, but on my other topic is the new problem (Realy thank you fot fixing the camera!)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.