0 votes

I am totally new to game developing and I am just trying to learn. I am sorry if I lack some terms, but I will try to explain what I want.

I was trying to figure out how to pick up an object and move it along with the mouse (drag and drop logic)

I found a code online that helped me with this, but it sets the position of the object (KinematicBody2D) to "getviewport().getmouse_position()" to make it move along with the mouse. I want to maintain the distance and the direction between the object and the mouse. For example, If I pressed and hold on the left corner of the object and dragged up the object will move in a parallel line.

object move relatively with the mouse

My code looks like this:

func _process(delta):
if (mouse_in && Input.is_action_pressed("left_mouse_click")):
    dragging = true
if (dragging && Input.is_action_pressed("left_mouse_click")):

    var newPosition = Vector2()
    if(position.x < get_viewport().get_mouse_position().x):
        newPosition.x = get_viewport().get_mouse_position().x - x
        newPosition.x = get_viewport().get_mouse_position().x + x
    if(position.y < get_viewport().get_mouse_position().y):
        newPosition.y = get_viewport().get_mouse_position().y - x
        newPosition.y = get_viewport().get_mouse_position().y + x
    position = newPosition

    dragging = false

-X is suppose to be the fixed distance between the initial mouse click and the object position. I didn't know how to obtain that so I use an int 16.

-mouse_in is set to true in mouse entered event and false in mouse existed.

This code seems to be doing what I want but if I move the mouse slowly and if I go fast the position of the object start changing to the right or left of the mouse position.

I would appreciate the help

Thank you

in Engine by (28 points)

1 Answer

0 votes

I fixed this issue with the following code. However, I am not very sure why this worked (mathematically) I just had one of this moment of (oh it worked!). Please if you can explain it I would appreciate it

func _input(event):
if event is InputEventMouseButton:
    if event.is_pressed() && mouse_entered:
        draggingDistance = position.distance_to(get_viewport().get_mouse_position())
        dir = (get_viewport().get_mouse_position() - position).normalized()
        click_on = true
        dragging = true
        dragging = false
        click_on = false
elif event is InputEventMouseMotion:
    if dragging && click_on:
        position = get_viewport().get_mouse_p


func _on_Player_mouse_entered():
mouse_entered = true
pass # replace with function body

func _on_Player_mouse_exited():
mouse_entered = false
pass # replace with function body
by (28 points)

No need for the bool "click_on". I think I just left it there by mistake.

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 Frequently asked questions and 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 [email protected] with your username.