Lag in-game when I look and move around

Godot Version
4.2.1

I’m having strange lag in my game and I cannot understand what’s causing me to have lag issues. I have a 3070 ti and an AMD Ryzen 7 5800X 8-Core Processor, so I don’t believe those are causing the lag. Here’s my code, could there be something in here that is causing the lag? I’m fairly new to this and tried to look into it, but I’m just unsure what could be causing it.

extends CharacterBody3D

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

const SPEED = 5.0
const JUMP_VELOCITY = 4.5
const SENSITIVITY = .03

var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)

func _ready():
Engine.max_fps = 60

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta

func _input(event):

if event is InputEventMouseMotion:
	head.rotate_y(deg_to_rad(-event.relative.x * SENSITIVITY))
	camera.rotate_x(deg_to_rad(-event.relative.y * SENSITIVITY))
	camera.rotation.y =clamp(camera.rotation.y,deg_to_rad (-100),deg_to_rad(100))
	head.rotation.x = clamp(head.rotation.x,deg_to_rad (-89),deg_to_rad(89))

if event.is_action_pressed("click"):
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
if event.is_action_pressed("ui_cancel"):
	Input.mouse_mode = Input.MOUSE_MODE_VISIBLE


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

var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction = (head.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()

I think input is causing the unsynchronization. try rewriting the code by physics process function.

1 Like

Try call move_and_slide() in physics process.

2 Likes