system
December 22, 2017, 1:05pm
1
Attention
Topic was automatically imported from the old Question2Answer platform.
Asked By
HarryCourt
Old Version
Published before Godot 3 was released.
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:Screen capture - 77eb831c1cde39fb304927e07a50ff55 - Gyazo
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
system
December 23, 2017, 10:11am
4
Reply From:
avencherus
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)
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
HarryCourt | 2017-12-23 11:55
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)
avencherus | 2017-12-23 14:00
I can’t pick it up still… Samme result in gif
HarryCourt | 2017-12-23 15:01
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:
Sleep the body for 1 frame before the first move takes place.
After one frame of sleeping issue position changes normally.
When the drag is released, unsleep the node.
Hope that helps.
avencherus | 2017-12-23 15:14