0 votes

I'm having some trouble with moving the camera2d with middle mouse button. What I did was get the mouse displacement vector and add the vector to the camera.position like in the below image
enter image description here

but the mouse is moving relative to the world map ( from (x0, y0) to (x1, y1) ). is there any other way to move the camera with the mouse such that the mouse always stays on the same position in world map (always (x0, y0) ) but camera move???

in Engine by (251 points)

1 Answer

0 votes

The sample is using the 'Input Map' in the 'Project Settings' (there 'move_map' is caused by 'Device 0, Middle Button'). The script is used by the Camera Node 'Camera2D':

extends Camera2D

var fixed_toggle_point = Vector2(0,0)

func _process(delta):
    # This happens once 'move_map' is pressed
    if Input.is_action_just_pressed('move_map'):
        var ref = get_viewport().get_mouse_position()
        fixed_toggle_point = ref
    # This happens while 'move_map' is pressed
    if Input.is_action_pressed('move_map'):

# slides the map around
func slide_map_around():
    var ref = get_viewport().get_mouse_position()
    self.global_position.x += (ref.x - fixed_toggle_point.x) / 80
    self.global_position.y += (ref.y - fixed_toggle_point.y) / 80

(alternatively here's the non-sliding version of this script for a more editor-like map movement)

by (28 points)

Hi! I have a question, How can I limit the movement, like: if position.x == x:
don't move, but if the camera came back to the "movement area" the cam can move again,

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.