Path2D and collision

NordNord Posts: 3Member
in 2D

Hello everyone!
I'm at it again learning more of the Godot engine after finishing my first game jam over the weekend and this time I have a 2D game with a base that moves along a Path2D.

With the current setup, the base moves as intended and all is fine and dandy, until the player steps in front of the moving base which makes it stop but it keeps on trying to get past if you leave it for a bit.

Both the base and the player are Kinematic2D and I'm wondering; how would one make it so the base has priority over the player, aka the player can't stop the base in its track and instead get pushed by the base?



  • TwistedTwiglegTwistedTwigleg Posts: 2,655Admin
    edited June 24

    In theory, you could use the following code (untested):

    # other code here
    # if using move_and_collide:
    collision_data = move_and_collide(velocity, Vector2.UP)
    if (collision_data != null):
        if (collision_data.collider is KinematicBody2D):
            # NOTE: this assumes the base KinematicBody2D is in a group called
            # base - this is just for making sure we only apply the base's velocity
            # and not the velocity of any KinematicBody2D
            if ("velocity" in collision_data.collider and collision_data.collider.is_in_group("base")):
                # apply the velocity of the collided object
                velocity += collision_data.collider.velocity
    # if using move_and_slide, it is more or less the same thing, you just need
    # to use the get_slide_collision function instead

    I have no idea if the above will work though, I just wrote it off the top of my head. That is how I might try to solve the issue initially. There are probably better ways to do it though. I would suggest looking at tutorials for moving platforms in 2D, as the basic idea for a moving platform should work with a moving base.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file