set_collision_layer_value() and set_collision_mask_value() works some of the time.

Godot Version

v4.7.stable.official [5b4e0cb0f]

Question

Hi everyone! I’m new to Godot and I’m trying to implement a drag-and-drop mechanic. I want an object to be able to pass freely through other objects while it’s being dragged, but once I drop it, I want it to start colliding with other objects again.

I’ve been trying to achieve this using set_collision_layer_value() and set_collision_mask_value(), but it’s inconsistent—it only works some of the time. I’m aware that I could simply toggle collision on and off, but that approach doesn’t really work for my needs.

Could you please point out what I might be missing?


This is what a Rigidbody2D looks like

image

Rigidbody2D script

extends RigidBody2D

@export var dragable_component: DragableComponent

var offset: Vector2

func _ready() -> void:
	dragable_component.dragged.connect(_on_dragged)
	dragable_component.dropped.connect(_on_dropped)


func _process(_delta: float) -> void:
	if dragable_component._is_dragged:
		global_position = lerp(global_position, get_global_mouse_position() - offset, 0.3)


func _on_dragged() -> void:
	offset = get_global_mouse_position() - global_position
	
	set_collision_layer_value(1, false)
	set_collision_mask_value(1, false)
	set_collision_layer_value(2, true)
	set_collision_mask_value(2, true)


func _on_dropped() -> void:
	set_collision_layer_value(1, true)
	set_collision_mask_value(1, true)
	set_collision_layer_value(2, false)
	set_collision_mask_value(2, false)

Dragable Component script

class_name DragableComponent extends Node2D

signal dragged
signal dropped

var _is_hovered: bool
var _is_dragged: bool


func _input(event: InputEvent) -> void:
	if _is_hovered and event.is_action_pressed("lmb"):
		_is_dragged = true
		dragged.emit()
		
	elif _is_dragged and event.is_action_released("lmb"):
		_is_dragged = false
		dropped.emit()


func _on_rigid_body_2d_mouse_entered() -> void:
	_is_hovered = true


func _on_rigid_body_2d_mouse_exited() -> void:
	_is_hovered = false

Video of the issue

issue

P.S. Apologies if any names or words seem silly; I am not a native speaker.

I had a similar problem in a project I was working on. I fixed it changing the scale of the collision shape to 0 when picking up, and then returning to 1 when it was released:

func enable_collision_shape() -> void:
	owner.set_freeze_mode(RigidBody2D.FreezeMode.FREEZE_MODE_KINEMATIC)
	owner.apply_central_force(Direction.get_random() * FORCE_INTENSITY)

	if tween: 
		tween.kill()

	tween = create_tween()
	tween.set_parallel(true)
	tween.tween_property(self, "scale", Vector2.ONE, COLLISION_SCALE_DURATION)

	collision_enabled.emit()


func disable_collision_shape() -> void:
	scale = Vector2.ZERO
	owner.set_freeze_mode(RigidBody2D.FreezeMode.FREEZE_MODE_STATIC)

	collision_disabled.emit()

Thanks for your response! That’s an interesting solution, but it essentially functions the same way as simply setting collision.disabled = true—and as I mentioned earlier, that doesn’t quite work for my needs. I specifically need the objects to be on different collision layers/masks so that I can control which objects they collide with and which ones they don’t.