Get_last_slide_collision and get_slide_collision_count only detecting once

Godot Version

4.2

Question

For some reason when I use get_last_slide_collision or get_slide_collision_count they will only detecting once and then stop all collisions completely.

My example is a enemy where the player will jump on it. Here is the simple code (both versions of it):

	hurtbox_body.move_and_slide()
	var hurtbox_collision = hurtbox_body.get_last_slide_collision()
	if hurtbox_collision:
		print("Collided with: ", hurtbox_collision.get_collider().name)
	hurtbox_body.move_and_slide()
	for i in hurtbox_body.get_slide_collision_count():
		var collision = hurtbox_body.get_slide_collision(i)
		print("Collided with: ", collision.get_collider().name)

It is basically a charbody2d trying to detect another charbody2d.

This is the effect with the code, the collision only happens once:

If I comment out the code, the collision will happen continually as normal:

Most probably because you call move_and_slide() from outside of CharacterBody2D.

Only the body itself should call its own move_and_slide(), and only once per frame, and only from _physics_process() or from functions that are called from _physics_process().

1 Like

Most probably because you call move_and_slide() from outside of CharacterBody2D.

Only the body itself should call its own move_and_slide(), and only once per frame, and only from _physics_process() or from functions that are called from _physics_process().

Thanks for the response, I tried this today and it was still giving me the 1 collision effect.

I did figure out the issue though, for some reason I needed to provide a reset to the velocity back to zero. The cool thing is I was able to do it from the other body! Part of me wonders if move_and_slide should always reset back to vec2 zero naturally? The fact it won’t collide again makes me wonder if it’s more a bug then preferred behavior. Here is the example code that works:

func _physics_process(delta):
	hurtbox_body.velocity = Vector2.ZERO
	hurtbox_body.move_and_slide()
	var collision = hurtbox_body.get_last_slide_collision()
	if collision:
		print("Collided with: ", collision.get_collider().name)

This has gotten me in another section of the code too, where the collision was holding even with waits because I wasn’t zeroing out the velocity before the check. Hopefully this can help others avoid similar situations.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.