Moving Camera smoothly with mouse_motion

mokamarsmokamars Posts: 1Member
edited August 2017 in Programming

Hi Guys !

I'm trying to implement a RTS MAP like control with the keyboard and a mouse (and touch screen later) in a 2D Game. It's more like a boardgame.

Currently, I have a Node (called "game") that represent the main screen of the game.
I have a Camera (child of the Node "game") where a script is attached to manage all inputs for the map.

I want to implement a simple click (anywhere) and drag the board until I release the mouse button (like in any of RTS-like map or boardgame).

I'm trying to use Mouse_motion to do this but I can't figure how I can use it.

For example :

  func _fixed_process(delta):
         if(dragging and InputEvent.MOUSE_MOTION):

  func _input(ev):
    if ev.type == InputEvent.MOUSE_BUTTON and ev.button_index == BUTTON_LEFT:
        pressed = ev.pressed
    if(ev.type == InputEvent.MOUSE_MOTION and not pressed):
        dragging = false
    if(ev.type == InputEvent.MOUSE_MOTION and pressed):
        dragging = true

But I have a problem with that because it's nots working correctly as you can see here

When I click anywhere on the screen the set_pos fucntion reposition my camera elsewhere before moving the camera.

I thought I could get the origin mouse position on the click and after that get the mouse position on every frame to calculate the new position of the mouse and create a vector2 that I could add to my camera position to move it smoothly.
I also thought that it is a good idea to stroe all of the input mangement in the camera script and move the camera. Do ou think it's better to move the borad or the camera with the mouse.
It works prefectly with the keyboard moving the camera

Anyone to help me ? I hope I'm clear.

Thank you

Sign In or Register to comment.