0 votes

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

in Engine by (295 points)

1 Answer

0 votes

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

by (1,318 points)

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?

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)

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 http://docs.godotengine.org/en/stable/learning/features/2d/2d_transforms.html?highlight=screen%20coordinates

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.

Categories