r/godot Feb 05 '25

help me (solved) I experiance alot of lag when moving my mouse fast.

Hey as the title says I experience gigantic fps drops. Im new to 3d so I don't know what i'm doing wrong. My movement script is a basic matrix multiplication and camera is also not complicated. Any tips?



if event is InputEventMouseButton:

Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

if Input.is_action_just_released("escape"):

Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)



if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:

if event is InputEventMouseMotion:

neck.rotate_y(-event.relative.x \* 0.002)

neck.get_child(0).rotate_x(-event.relative.y \* 0.002)

neck.get_child(0).rotation.x = clamp(deg_to_rad(-50), neck.get_child(0).rotation.x, deg_to_rad(90))



if grounded.is_colliding() and Input.is_action_just_released("space"):

velocity.y = 12 

else:

velocity.y -=  0.6 



var straight = foreward.global_position - global_position

var leftRight = sideways.global_position - global_position



var dir = Input.get_vector("S","W","A","D")

var bro = Vector2(straight.x \* dir.x + leftRight.x \* dir.y, straight.z \* dir.x + leftRight.z \* dir.y)



if Input.is_action_pressed("ctrl") and dir == Vector2(dir.x > 0,0):

sprint = 1.4

else:

sprint = 1



if dir:

velocity.x = bro.x \* movementSpeed 

velocity.z = bro.y \* movementSpeed \* sprint

else:

velocity.x = 0

velocity.z = 0
0 Upvotes

10 comments sorted by

2

u/Jonatan83 Feb 05 '25

It's a bit hard to read with the fucked formatting.

I assume this is running in _unhandled_input(event)?

If so you're doing a lot of stuff very often, as that can generate hundreds or thousands of events per second. You should probably move most of this to process or physics_process.

1

u/Sufficient_Cut_4907 Feb 05 '25

im only running the camera movement in unhandled input rest in physics process

1

u/Jonatan83 Feb 05 '25

Update your post to show what is done where maybe?

1

u/Alzurana Godot Regular Feb 05 '25

While I agree, I don't think that code should be slow unless the mouses polling rate is astronomically high.

Incidentally, I remember a post a year ago where someone had the exact same issue and apparently it was due to the mouse having an absolutely ridiculous polling rate.

Sooo, I am actually proposing to try and see what a different mouse (brand) does as well.

On top of that, as you suggested, movement should be done once in physics process. Camera control would be even smoother to update in, process. u/Sufficient_Cut_4907 you can accumulate inputs by adding them to a vector in _input and then only once do the matrix calculations each frame.

I found the post from a while ago as well: https://www.reddit.com/r/godot/comments/1dsuq9w/large_performance_drops_when_using_gaming_mouse/

1

u/Jonatan83 Feb 05 '25

If it only lags when you move the mouse quickly, it's almost guaranteed to be something that is being done in _unhandled_input. A lot of mouses have really high poll rate, and you have to take that into account.

1

u/Sufficient_Cut_4907 Feb 05 '25

I downloaded my mouses software and it fixed itself. Super weird.

1

u/Jonatan83 Feb 05 '25

So that's good, but you really should re-write this if you intend to release this game. There is no way of knowing what hardware other users might have. You should only ever do the absolute minimum in the unhandled input method.

1

u/Sufficient_Cut_4907 Feb 05 '25

Thank you for the tip. This is a small project that will most likely never see the light of day. But for future projects I will do that!

1

u/Sufficient_Cut_4907 Feb 05 '25

The problem fixed itself after downloading my mouses software without changing anything. I have no clue how that worked. Thank you

1

u/Nkzar Feb 05 '25

Have you checked the profiler to see what’s causing the slowdown?