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)
in Engine by (33 points)

1 Answer

+3 votes
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)
by (20,917 points)
selected by
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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.