[EXAMPLE] Drag camera with the mouse


#1

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


Help! Circle object and scrolling menu
#2

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


#3

ddabrahim

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


#4

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…


#5

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?


#6

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:


#7

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

I will wait than. thank you.


#8

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):
https://gametemplates.itch.io/gdevelop-examples

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


#9

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