Could someone please translate this Godot 2 code to Godot 3 code?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jackson King
  1. if is_colliding():
  2. (Indent) var normal = get_collision_normal()
  3. (Indent) var object = get_collider()
  4. (Indent) var objectParent = object.get_parent()
  5. (Indent) if normal == Vector2(0, 1):
  6. (Indent) (Indent) objectParent.queue_free()

I’m using move_and_slide to move if that helps.

:bust_in_silhouette: Reply From: kidscancode

Note: You can format code using four spaces, or by clicking the “Code Sample” button in the editor above.

move_and_slide() moves the body along surfaces. This means that if it hits, for example, in a corner, it’s possible that one move could hit more than one collider. For that reason, you have get_slide_count() which tells you how many collisions occurs, and get_slide_collision() which obtains the collision information in the form of a KinematicCollision2D object.

So the best example, I can give for the code above, without more context would be:

# after move_and_slide:
for collision_id in get_slide_count():
    var collision_data = get_slide_collision(collision_id):
    if collision_data:
        var normal = collision_data.normal
        var object = collision.collider
        var object_parent = object.get_parent()
        if normal == Vector2(0, 1):
            object_parent.queue_free()