Crush Physics for Dying

Godot Version

4.2

Question

So I want to make my 16x16 2D player die when they are crushed between two surfaces. For example, let’s say the player gets caught between a moving object and the floor. If the player is touching both the object AND the floor, the player should die. However, my solution with raycasts in each direction shove the player into the floor and THEN kill them, which we can all recognize as lazy if we saw this in an actual game. However, how do I do this???

this is all the relevant code for the death process. I have tried shifting some scripts around, but it doesn't seem to work.

var dead = false

@onready var animation = $AnimatedSprite2D
@onready var up = $Raycasts/Up
@onready var down = $Raycasts/Down
@onready var left = $Raycasts/Left
@onready var right = $Raycasts/Right

func _physics_process(delta):
	if up.is_colliding() and down.is_colliding() or left.is_colliding() and right.is_colliding():
		death()

func death():
	get_tree().paused = true
	dead = true
	swordsprite.visible = false
	animation.play("death")

Use a shape cast to make it easy.

How about use an Area2D wrap your moving object, then make a script to tell player that the object is squeezing the player in body_entered connect funciton. And kill player if the player has been squeezed.

I don’t know how to use shape casts, and there are no tutorials for it. How could this be done?

Found a solution! I changed how the death() function worked and made the hitboxes of objects bigger, so now when the player touches an object, death() is triggered and the player force-checks the raycasts.

func death():
	up.force_raycast_update()
	down.force_raycast_update()
	if up.is_colliding() and down.is_colliding():
		get_tree().paused = true
		dead = true
		swordsprite.visible = false
		animation.play("death")
	left.force_raycast_update()
	right.force_raycast_update()
	if left.is_colliding() and right.is_colliding():
		get_tree().paused = true
		dead = true
		swordsprite.visible = false
		animation.play("death")

Thank you and sorry. The past few days I’ve been sick, so I’ve been looking for quick and easy ways to do complex stuff while I’m not level-headed. So rather than using my braincells, I tried to steal yours.
Which is fair because I only have 3. You can spare a few hundred of your 86 billion neurons, I’m sure.

1 Like