0 votes

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)
asked Jan 3 in Engine by wyy2 (31 points)

1 Answer

+1 vote
Best answer

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)
answered Jan 3 by kidscancode (17,712 points)
selected Jan 4 by wyy2
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.