0
\$\begingroup\$

enter image description here

func move(delta): # move the player linearly

#   var col_info = move_and_collide(velocity * delta)
#   var remaining_vel = Vector2()
    var remaining_vel = move_and_slide(velocity)
    var col_info = get_slide_collision(0)

    handle_collision(delta, col_info, remaining_vel, false)


func handle_collision(delta, col_info, rem_vel, slide = false):

    handle_nonslide_collision(delta, col_info, rem_vel)

func handle_nonslide_collision(delta, col_info, rem_vel):
    if(col_info):
        var bodyHit = col_info.collider
        if(col_info.collider.get_class() == "KinematicBody2D"):

            if(bodyHit.is_in_group("KinematicBox")):
                var bodyHitTex = bodyHit.get_node("Sprite").get_texture()
                var bodyHitSprite = bodyHit.get_node("Sprite")
                var Tex = $Sprite.get_texture()
#               var side = Global.at_what_side(bodyHit.position, bodyHitTex.get_width() / bodyHitSprite.hframes, bodyHitTex.get_height() / bodyHitSprite.vframes, position, tileSize, tileSize)
                var side = bodyHit.side_touched
                match side:
                    "Right":
                        bodyHit.move_and_collide(Vector2(col_info.get_remainder().x, 0))
                        my_side = "Right"
                    "Left":
                        my_side = "Left"
                        bodyHit.move_and_collide(Vector2(col_info.get_remainder().x, 0))
                    "Up":
                        my_side = "Up"
                        bodyHit.move_and_collide(Vector2(0, col_info.get_remainder().y))
                    "Down":
                        my_side = "Down"
                        bodyHit.move_and_collide(Vector2(0, col_info.get_remainder().y))
                    _:
                        my_side = "N/A"
            else:
                bodyHit.move_and_collide(col_info.get_remainder())
                pass

            col_info = move_and_collide(col_info.get_remainder())
            if(col_info):
                v_length = velocity.length() - col_info.get_remainder().length()

        elif(col_info.collider.get_class() == "RigidBody2D"):
            col_info.collider.add_force(Vector2(), velocity * delta)

            bodyHit.col = true
            col_info = move_and_collide(col_info.get_remainder())
            if(col_info):
                v_length = velocity.length() - col_info.get_remainder().length()
    else:
        v_length = velocity.length()

    col_info = null
    pass
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.