How to detect multiple terrain collisions at once with CharacterBody2D

Godot Version

4.2

Question

I am making a tile-based platformer and I want to know if it is possible to detect multiple types of “terrain” which are being stood on at once within a CharacterBody2D script.

image

In my particular scenario, the player (CharacterBody2D) is standing over both a Tilemap and a RigidBody2D at the same time, which are perfectly flush with each other. get_slide_collision_count() returns “1” in this scenario, but I would like to iterate over the collisions of both objects the player is standing on, whatever they may be. This also applies when the player is running into an object horizontally, it will only count 1 collision despite them being on the floor at the same time. What can I do about this?

1 Like
for i in range(get_slide_collision_count()):
    var colliding_with = get_slide_collision(i).collider
    # if colliding_with == ...

My code already looks like this, the issue is get_slide_collision_count() returns 1 while standing over both terrain types AND when touching a wall while on the ground. Instead of detecting collisions with both terrains, it is only detecting collisions with one of them (usually the TileMap, it seems like wall collisions get priority over ground collisions).

For reference this is what my code looks like.

if move_and_slide():
		for i in range (get_slide_collision_count()):
			if i < get_slide_collision_count(): #Since move_and_slide() might be called again during this
				var coll:KinematicCollision2D = get_slide_collision(i)
				var collider = coll.get_collider()
				if collider != null:
					if Library.getEntityType(collider) == Enums.EntityType.BLOCK:
						var normal = coll.get_normal()
						var normalAngle = round(rad_to_deg(normal.angle()))
						collider.onPlayerCollide(self, normal, normalAngle)
			else:
				break