Movement values change based on screen resolutions

Godot Version

4.2.2

Question

I have a move_and_slide script for moving a CharacterBody2D. I recently noticed that on a different monitor with a different resolution the movement is much faster. (The base monitor where I code and test is 3440x1440. Then I tried on a 1920x1080). I did notice the delta value is different - probably due to fps difference?
The movement script is trying to work off of swiping on a touch screen.
I tried removing the delta variable, I tried using _physics_process and also tried to implement a resolution check, after which I change the absolute values in the movement script based on the relative difference between the two screen resolutions. But the issue presists.
I’m quite new to this, so it might be something quite simple - but I ran out of ideas.
Any thoughts?

Code:

extends CharacterBody2D
# Variables to set up movement
var lenght := 5 # absolute value !
var StartPos: Vector2
var CurPos: Vector2
var swiping := false
var threshold := 10 # absolute value !
var direction: Vector2
var move_amount := 100 # absolute value !
func _process(delta):
# Movement script
	if Input.is_action_pressed("ScreenTap"):
		if !swiping:
			swiping = true
			StartPos = get_global_mouse_position()

	if Input.is_action_pressed("ScreenTap"):
		if swiping:
			CurPos = get_global_mouse_position()
			if StartPos.distance_to(CurPos) >= lenght:
				if abs(StartPos.y - CurPos.y) >= threshold:
					if StartPos.y > CurPos.y: #downward
						direction = Vector2(0,-move_amount)
						velocity = direction * Global.speed * delta
						move_and_slide()
						
					if StartPos.y < CurPos.y: #upward
						direction = Vector2(0,move_amount)
						velocity = direction * Global.speed * delta
						move_and_slide()
					
					swiping = false
				if abs(StartPos.x - CurPos.x) >= threshold:
					if StartPos.x > CurPos.x: #leftward
						direction = Vector2(-move_amount,0)
						velocity = direction * Global.speed * delta
						move_and_slide()
						
					if StartPos.x < CurPos.x: #rightward
						direction = Vector2(move_amount,0)
						velocity = direction * Global.speed * delta
						move_and_slide()
						
					swiping = false
	else:
		swiping = false

_process() is called every frame, and delta is the time it took (in seconds) to draw the frame (the time elapsed since the last _process() call precisely)

So yes, doing it like this will result in your movement being framerate dependant.

For the speed to be consistent you want to use _physics_process() and use delta in the calculation.

_physics_process() is called at a fixed rate, that you can change in the settings.

Since you tuned your speeds to work for your framerate instead of the physics call rate, you will probably have to increase the values to have a similar feel as you had before

PS: if you want to check without switching screens, you can change the maximum FPS of the game with a framecap, either in code by changing Engine.max_fps or in the settings

1 Like

Thanks for the quick response!
Using _physics_process() seem to have worked.
I did try that earlier but I must have miss-typed something because it didn’t work then - but I tried again on your recommendation and now it’s all good!

1 Like