Help needed with a moveable box

Godot Version

4.5.1 stable

Question

Hello all! This is my first post here, and after scrolling the forums for a bit of time, I can’t seem to find a solution to this. I have a rigidbody2d that acts as a click and drag box inside of my puzzle platformer, and the code works fine inside of it’s own scene. However, once outside of its scene (once it’s instanced) the click and drag no longer works. I saw that control nodes could be an issue, but there is only a colour rect so I don’t believe it’s the issue (please correct me if I’m wrong!) and the block is a child of the main world scene, not nuzzled in any other child of the world. Thanks for the help!

extends RigidBody2D


var move_box = false

func _ready() -> void: # Check if the area exists
	print("Area node: ", $MouseEnterArea)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if Input.is_action_pressed("move_box") and move_box == true: # On left clicking, and being in the box area, move
		position = get_global_mouse_position()

func _on_mouse_enter_area_mouse_exited() -> void:
	linear_velocity.y = 0 # So that gravity resets
	move_box = false # Can no longer move the box

func _on_mouse_enter_area_mouse_entered() -> void:
	move_box = true

You should move the RigidBody2D only in the _integrate_forces().

Ah okay, thanks for the feedback! I’ll consider it for my next rigid body, for now I found that the colour rect was causing problems, and so removing it seems to be the solution, I think I have to use the _integrate_forces() to override the colour rect somehow…