Is there a mouse Click and move event?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ondesic
:warning: Old Version Published before Godot 3 was released.

I was wondering, many engines have a way to catch a mouse click and drag. Does Godot have a for instance a event.dragged ?

:bust_in_silhouette: Reply From: YeOldeDM

It does have drag-and-drop functionality.
Take a look at the Drag & Drop GUI demo project.

I’ll take a look.
I am looking to have an object only move towards the mouse if the left button in held down. Would that be something different that drag and drop?

ondesic | 2017-04-08 06:29

Yes it is.

You will want to take the subtract the object position from the mouse position to get a vector. Normalize it, multiply in the speed, and then add it the existing object position. And repeat this every frame.

Something like:

var heading = (mouse_pos - object_pos).normalized()
var new_pos = object_pos + heading * speed * delta
object.set_pos(new_pos)

avencherus | 2017-04-08 10:01

I’m affraid it’s more complex than that: mouse coordinates are given in the screen space and in your case you are interested by the global coordinates in your game ! Except if your camera is static and your screen coordinates ARE your world’ coordinates… Otherwise I guess you should use Viewport and canvas transforms — Godot Engine (stable) documentation in English

thomas_laverne | 2017-06-24 13:06