0 votes

I have a script that once a player picks up a RigidBody2D, it will pick it up and follow the mouse. However, Once I let go, the sprite doesn't move and the Collision stays at the place it originally landed. Once I click in the place it landed, the sprite moves back. It is currently in 'Rigid' mode.

Here's the gif:https://gyazo.com/77eb831c1cde39fb304927e07a50ff55

Here's the code:

extends RigidBody2D


# Variables that make it work
onready var spawner = get_node("/root/Scene/Player/Spawner")
onready var canDrag = false

# Stats
var massOutput = get_mass()
var weightOutput = get_weight()

func _ready():
    pass



func _physics_process(delta):

    #var canGrab = mouse_checker_node.is_free

    if (Input.is_action_pressed("ui_grabItem") && canDrag == true):
        var mousePos = get_global_mouse_position()

        self.set_position(mousePos)

    elif (Input.is_action_just_released("ui_grabItem")):
        canDrag = false

func _on_Item_mouse_entered():
    canDrag = true


#func _on_Item_mouse_exited():
#   canDrag = false
in Engine by (162 points)
edited by

1 Answer

0 votes

Haven't tried it yet in 3.0, but to gracefully move a RigidBody2D, you have to put it to sleep, defer for one frame, then move it and reawaken it.

set_sleeping(true)
call_deferred("move_to", pos)


func move_to(pos):
    set_position(pos)
    set_sleeping(false)
by (5,232 points)

It didn't fix it, but that might be because of my lousy programming knowledge, care to take a look?

extends RigidBody2D


# Variables that make it work
onready var spawner = get_node("/root/Scene/Player/Spawner")
onready var canDrag = false
onready var mPos = get_local_mouse_position()

# Stats
var massOutput = get_mass()
var weightOutput = get_weight()

func _ready():
    pass


func _physics_process(delta):


    #var canGrab = mouse_checker_node.is_free

    if (Input.is_action_pressed("ui_grabItem") && canDrag == true):
        var mousePos = get_global_mouse_position()

        self.set_position(mousePos)

    elif (Input.is_action_just_released("ui_grabItem")):
        canDrag = false

func _on_Item_mouse_entered():
    canDrag = true




func move_to(mPos):
    set_sleeping(true)
    call_deferred("move_to", mPos)

    set_position(mPos)
    set_sleeping(false)

#func _on_Item_mouse_exited():
#   canDrag = false

Sorry, I omitted some context to focus on the specific lines.

You want to use it like this (Edited for better context):

func move(mousePos):
    set_sleeping(true)
    call_deferred("move_to", mousePos)


func move_to(mPos):
    set_position(mPos)
    set_sleeping(false)

I can't pick it up still... Samme result in gif

Sorry, bad context once again, I'm just trying to show the idea, but slapped it mindlessly into your "drag" code.

The logic/algorithm for using this with dragging:

  1. Sleep the body for 1 frame before the first move takes place.
  2. After one frame of sleeping issue position changes normally.
  3. When the drag is released, unsleep the node.

Hope that helps.

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.