How to get collided object name (colliding with floor)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By wyy2

Hello,
I’m started learning godot & I have a question. I found script which returns collided object name. But it works only when moving left or right. For example if you jump and gravity pulls you down on the floor, there is no collision response.

motion.y += GRAVITY

if Input.is_action_pressed("ui_right"):
	motion.x = SPEED
elif Input.is_action_pressed("ui_left"):
	motion.x = -SPEED
else:
	motion.x = 0
	
if is_on_floor():
	#print("On floor")
	if Input.is_action_just_pressed("ui_up"):
		motion.y = JUMP_HEIGHT
else:
	print("Not on floor")

for i in range(get_slide_count() - 1):
    var collision = get_slide_collision(i)
    print(collision.collider.name)

motion = move_and_slide(motion, UP)
:bust_in_silhouette: Reply From: kidscancode

This is wrong:

for i in range(get_slide_count() - 1):
    var collision = get_slide_collision(i)
    print(collision.collider.name)

This means that if there is only one collision (such as on the floor), the loop will run zero times. Change it to:

for i in get_slide_count():
    var collision = get_slide_collision(i)
    print(collision.collider.name)