I want my camera2d to move to the right or left if you move your cursor to the edge of the screen. My current code is this:

extends Camera2D
var spd = 10
func _process(delta):
	var mouse = get_viewport().get_mouse_position()
	if mouse.x > 0 and mouse.y > 0 and mouse.x < 100 and mouse.y < 600:
		position.x -= spd
	elif mouse.x > 924 and mouse.y > 0 and mouse.x < 1024 and mouse.y < 600:
		position.x += spd

This works but it isn't the best because it suddenly jumps when you go into the movement range whereas I would like it if the closer to the edge of the screen the cursor is the faster the screen moves.

Thanks.

Welcome to the forums @hjbdos!

Maybe try something like this:

extends Camera2D
var spd = 10
var radius_required_to_move = 100
func _process(delta):
	var mouse_position = get_global_mouse_position()
	var mouse_delta = mouse_position - global_position
	if (mouse_delta.length() >= radius_required_to_move):
		position += (mouse_delta / radius_required_to_move) * spd * delta

I have no idea if it will work though, but I think it should give you faster movement the further the cursor is away from the center of the screen, once it's outside of the given radius.

2 years later