I have a problem with the movement of the player and the camera

I’m just now starting to learn first-person. When the player collides with an object, the game starts to lag. Additionally, the camera’s X rotation cannot stay within the range of -90.0 to 90.0 degrees.

Here is the player’s code:

 extends CharacterBody3D

@export var speed : float = 10.0
@export var mouse_sensitivity := 0.001
@onready var head: Node3D = $Head
@onready var shooting_ray_cast: RayCast3D = $Head/ShootingRayCast

func _ready() -> void:
 	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _physics_process(delta : float) -> void:
    if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
     	#moving
	    var input_directions := Input.get_vector("left",'right',"forward","backwards")
	    var direction := transform.basis*Vector3(input_directions.x, 0, input_directions.y)
	    velocity = direction*speed
	    move_and_slide()

func _input(event : InputEvent) -> void:
    if Input.is_action_just_pressed("hide_and_show_the_mouse"):
	    Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if Input.mouse_mode Input.MOUSE_MODE_VISIBLE else Input.MOUSE_MODE_VISIBLE

    if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
	    move_camera(event.relative)

func move_camera(relative :Vector2) -> void:
    rotate_y(-relative.x*mouse_sensitivity)
    head.rotate_x(-relative.y*mouse_sensitivity)
    head.rotation_degrees.x = clampf(head.rotation_degrees.x,-90.0,90.0)

There doesn’t appear to be anything inherently wrong with your code.

  • What does your node tree look like?
  • What do you mean by lag; do you have a video?
  • Will any collision cause this lag?

I would need more information to get good insight into the problem.

"For some reason, when I try to add a video, it says: 'Sorry, new users cannot upload attachments.

Well – Youtube is a free service that you can use to upload videos. Maybe you could share a link to one when you’ve uploaded it?