Camera movement by mouse drag exceeding limit

Godot Version

4.3

Question

Hi! I am new to Godot and I want to make the camera move around the map/terrain by dragging it using the mouse in which I was able to do but it always exceed the camera limits when I tested it. The thing is I implemented this feature not with ‘InputEventMouseButton’ since I don’t want this feature to get activated by whatever mouse button I press. I created an input called “drag_camera” binded to the right mouse button. I also make the camera move around by changing its offset property through script but I found out that offsets can go past the camera’s limits. So I thought that maybe doing this:

var move_camera = Vector2(0, 0)

func drag_cam_around() -> void:
	var top_left_offset = Vector2(0, 0)
	var bottom_right_offset = Vector2(612, 338)
	
	if Input.is_action_just_pressed("drag_camera"):
		move_camera = get_global_mouse_position()
	elif Input.is_action_pressed("drag_camera"):
		var gap = move_camera - get_global_mouse_position()
		if offset >= top_left_offset and offset <= bottom_right_offset:
			offset += gap

This is the code, this is the code where it first worked but it go way past the camera limits:

var move_camera = Vector2(0, 0)

func drag_cam_around() -> void:
	if Input.is_action_just_pressed("drag_camera"):
		move_camera = get_global_mouse_position()
	elif Input.is_action_pressed("drag_camera"):
		var gap = move_camera - get_global_mouse_position()
		offset += gap

So if anyone have a workaround or something, thanks in advance!

What exactly do you mean by camera limits? As far as I understand, you’re trying to make sure the camera stays between top_left_offset and bottom_right_offset.

But honestly, I think there’s a better approach then trying to use the offset property. If we use the _input(event) function instead of the Input Singleton (The thing you’re using right now), we’ll get notified each time an input has happened. You mentioned you don’t want to start the drag when any mouse button is pressed, but that’s not what’s going to happen here. You can check the event we get from _input() with event.is_action_pressed("drag_camera"). Then, we can check if the event is a InputEventMouseMotion with if event is InputEventMouseMotion. And look at that: It has a relative property. We don’t need to track how the camera moved like you’re doing with the gap variable, Godot does it for us! You can just apply this value to the current position of the camera: camera.global_position -= event.relative. We need to subtract the value because the camera needs to negate the mouse motion. You can think of it like this: When trying to drag the screen, we want the mouse position to stay at the same place relative to the game world. If we click on a house, then drag the camera, and then let go, the mouse pointer should still be on the hose, because the map and the cursor both moved. If you can’t visualize this (I kinda explained it bad) you can just change this to a + and see the difference for yourself.

1 Like

I started looking into it a bit more earlier and it turns out I had a misunderstanding on how to use “InputEvenMouseButton” and it turns out I can just do this

if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_RIGHT:

Still, thanks!