Area2D and collisions with RigidBody logic problem

Godot Version

Godot 4.3

Question

Hello guys and thanks for reading me !

I’ve made a game for the GMTK game jam and struggled with a logic problem in my code. I managed to finish the game but, the logic is still messy and buggy and I just can’t figure how to fix this. Here’s my problem :

The game is simple. You pick items (rigidbodies) to create a structure, the goal is to make the structure high enough, and stable to trigger an animation dezooming the camera, and the new items are bigger and bigger, etc. So my problem is with the logic with this “Heigth Limit”.

I though the easiest way would be to create a Area2D to set the limit, and then, when the structure collides with it, and is stable, trigger the animation. So here’s my code :

func _on_height_body_entered(body):
	inHeightZone = true
	while inHeightZone && body.held == true or body.linear_velocity.y > 1 or body.linear_velocity.y < -1:
		await get_tree().create_timer(3).timeout
		print("moving")
	if inHeightZone && body.held == false && body.linear_velocity.y < 1 && body.linear_velocity.y > -1:
		print("entered")
		anim.play("cameradezoom1")
		height.queue_free()
		cam_move = true
		stage1over.emit()
		
	else:
		cam_move = false
		
		
func _on_height_body_exited(body):
	inHeightZone = false

Because I don’t want the animation to trigger as soon as a item hits the Area2D, I’ve set a timer. I know it’s not a good idea, but I don’t know what else to do. Also, it creates other problems:

If a body1 enters the zone, and then another body2 enters the zone before the first timer runs out, it can trigger the animation even if the body2 is still moving and “Held”. Because “body” is only one var. It works most of the time but, not in a good way.

So my goal is clear. I want a way to check at anytime if an item is
-Not held
-Not moving
-“Collides” with the height zone.

Pardon my english, I hope it’s clear enough, if not just tell me. Thanks again for reading, I hope you have a good day.

1 Like

I would toggle the collision layer.

So that the layer is only for “unheld” and “not moving” items.

Make two booleans “moving” and “held”. Toggle them accordingly.

When both are false, toggle the collision layer on, otherwise turn it off.