[EXAMPLE] Drag camera with the mouse

I needed the feature in my project to drag and move the camera with the mouse as in a strategy game for example.
I experimented with different ideas, but finally I decided to do it the hard way and use some math and since I’m not very good with numbers, it surprised me how well it actually works and I decided to share it as an example in case someone else also need such functionality.

Of course there must be a better and smarter way to do this, feel free to improve it anyway you can and please share it :slight_smile:

Example:
gametemplates.itch.io/template- … with-mouse

1 Like

Here is my version:
CameraDragging.zip (4.06 KB)
Easier to read, but more restricted… the balance of life :neutral_face:

ddabrahim

How can I add limits to the camera in your extension? (in human language please, I’m not a coder)

You can check the position of the center point of the camera on the X and Y axis.
So, what you could do is before moving the camera on the X and the Y axis, add the condition to check if the X and Y position of the camera is within the limits you want to set.

For example if X position of camera is > 0 and < 1600 then move the camera on the X axis…

hi.
I saw an another topic you told this. I tried to do this before I wrote my comment. may be I was successful, but this isn’t work correctly. or I did something wrong. because after the camera reaches the desired limit it stops, and doesn’t ever move again.

so. would you be so kind and show me wich line is exactly the “before moving the camera” , and how it will move again after it stopped once?

You can try to reset the position of the camera when reaches the limit.
For example if X position of camera is <= 0 then Do = 1 to X position of camera.
But depends on how fast you move the camera it may going to cause the camera to shake a little.

I was about to suggest to use tween animation to move the camera to position but it seems tween is not implemented for the camera, so what you can do then is move an object using tween and make the camera follow the object, it should give you more smooth experience if that would be a problem.

Sorry, I don’t have time right now to look in to this, I’ll take a look later this week if you can’t figure this out by then and you get no help from others :+1:

1 Like

the <= 0 then Do = 1 doesn’t help. (may be it is in wrong place).

I will wait than. thank you.

Hi.

I have updated the example on itch.io so you can drag and move the camera only within limits.
You can download it from here (GDevelop5_drag-camera-with-mouse.zip):
GDevelop Examples by GameTemplates

Hope it helps. Any problem let me know :+1:

1 Like

Thank You very much. it is perfect! You helped me a lot! Thank You.

I am about to use this extension but my objects are also dragable. so while moving the objects toward the target position screen also get scrolled.
Please suggest how to stop screen scrolling during object dragging behaviour

Hello!

Please do not bump a multi-year old thread, especially when your use case is slightly different than the original post. I’d recommend writing up what you’re trying to accomplish and showing your events in a new thread, so people can better assist.

Thank you.

1 Like