Simple bug when interacting with an object

Godot Version

4.3

Question

I’m trying to make it so that the player can push an object, pick it up and stand on it. it seems to have worked, but if the player first stands on an object, and then tries to interact with it, then the object remains in place.

My object is rigidbody2d and has a layer 3 and a mask 1, 3. The player has a layer 1 and a mask 1, 2.
So that the player can stand on it and not fall through, I made a small collision of the player, which, when colliding with an object, does this:

func _on_area_2d_body_entered(body):
	if body.is_in_group("item"):
		body.collision_mask = 3
		body.collision_layer = 3

when the interaction disappears:

func _on_area_2d_body_exited(body):
	if body.is_in_group("item"):
		body.collision_mask = 2
		body.collision_layer = 2
		body.collision_mask = 1
		body.collision_layer = 1

I guess the problem is that after a collision, the object layer does not return to its initial state.
It seems so simple, but I’ve already tried everything and I couldn’t solve it. Please help me and explain what’s wrong

This is how it should work:

	body.set_collision_layer_value(1,false)#diasble collision layer 1
	body.get_collision_layer_value(1)#returns false
	
	body.set_collision_layer_value(1,true)#enable collision layer 1
	body.get_collision_layer_value(1)#returns true
	
	body.set_collision_mask_value(1,false)#disable mask layer 1
	body.get_collision_mask_value(1)#return false
	
	body.set_collision_mask_value(1,true)#enable mask layer 1
	body.get_collision_mask_value(1)#return true

But be careful, by disabling active mask and layer, your function _on_area_2d_exited will either never trigger or trigger instantly. So you need to keep at least one layer active.

1 Like

Thank you very much!!!

1 Like