@TwistedTwigleg said:
Where are you executing this code? It could be that the physics engine and the code are out of sync, which causes the physics engine to "take control" and snap the object back into where the physics engine thinks it should be. I would recommend manipulating the RigidBody2D node in _physics_process
if possible, to avoid syncing issue.
Though, looking at the previous posts, it seems that is not the issue. Have you tried modifying global_position
/global_transform
? I doubt it would make a difference, but it might be something to experiment with.
A hack workaround that might work is code like the following:
get_node("Coins").Coin1.can_sleep = true # may not be needed.
get_node("Coins").Coin1.sleeping = true
get_node("Coins").Coin1.transform = Transform2D(0, Vector2(180, 180))
get_node("Coins").Coin1.sleeping = false
Not sure if that would fix the issue, but making the RigidBody2D sleep for a second might work around the position snapping back into place.
Unfortunately it didn't work. It acts like the second code below.
Actually previous solution (changing it to static before positioning) partly worked but it has some other problems.
The following code changes its position correctly but then I need to change it to rigid again to interact with the object:
func _physics_process(delta):
if freekick_formation == true:
get_node("Coins").Coin1.mode = RigidBody2D.MODE_KINEMATIC
get_node("Coins").Coin1.global_transform = Transform2D(0, Vector2(180, 180))
formation_done = 1
freekick_formation = false
When I change the object mode to Rigid again after setting its position like below, it just moves the object for a moment (like flickering) and then move it to the previous position as soon as it becomes rigid:
func _physics_process(delta):
if freekick_formation == true:
get_node("Coins").Coin1.mode = RigidBody2D.MODE_KINEMATIC
get_node("Coins").Coin1.global_transform = Transform2D(0, Vector2(180, 180))
formation_done = 1
freekick_formation = false
if formation_done == 1:
get_node("Coins").Coin1.mode = RigidBody2D.MODE_RIGID
formation_done = 0