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)