This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Prevent "drag" against StaticBody2D when moving along the surface

XascoriaXascoria Posts: 3Member

So I'm not entirely sure what settings this is considered as, allow me to explain.

For the example, I have a StaticBody2D located directly at the North of my KinematicBody2D (The Player), I'm using move_and_collide for the player's movement. If I walk straight up, I will get stopped when I hit the StaticBody2D, which is the expected behavior. If I then stop moving straight up but instead walk left or right. I will move to the left/right with the normal speed, which is also expected. The question I'm asking is that after I get stopped by the wall, if I continue to move_and_collide to both top and left, instead of moving left at a normal speed, the KinematicBody2D will slow down considerably and seems to get "dragged" across the surface of the StaticBody2D, and what I wanted is that it would simply ignore the upward force and just move to the left normally. Putting fiction to 0 doesn't change anything, so anyone knows how can I fix this?

Thanks for your time.

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 2,561
    Accepted Answer

    While I'm not sure, my guess on what is happening is that your velocity is pointing to the upper left corner, rather than two velocities (one up and one left). In this case, because the velocities are combined, the amount of force hitting the StaticBody2D is lost via the move_and_collide function, leaving less than half of the expected velocity in the direction that does not collide (if that makes sense).

    To fix this, I would call move_and_collide separately for each axis you want to move in. For example (untested):

    # I will assume the velocity is stored in a variable called velocity
    if (velocity.x != 0):
        # Only move on the X-axis
        move_and_collide(velocity * Vector2(1, 0), Vector2.UP)
    if (velocity.y != 0):
        # Only move on the Y-axis
        move_and_collide(velocity * Vector2(0, 1), Vector2.UP
    

    Then because the movement is separated per axis, collision on one axis should not affect the other, giving the desired results.

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin
    Accepted Answer

    While I'm not sure, my guess on what is happening is that your velocity is pointing to the upper left corner, rather than two velocities (one up and one left). In this case, because the velocities are combined, the amount of force hitting the StaticBody2D is lost via the move_and_collide function, leaving less than half of the expected velocity in the direction that does not collide (if that makes sense).

    To fix this, I would call move_and_collide separately for each axis you want to move in. For example (untested):

    # I will assume the velocity is stored in a variable called velocity
    if (velocity.x != 0):
        # Only move on the X-axis
        move_and_collide(velocity * Vector2(1, 0), Vector2.UP)
    if (velocity.y != 0):
        # Only move on the Y-axis
        move_and_collide(velocity * Vector2(0, 1), Vector2.UP
    

    Then because the movement is separated per axis, collision on one axis should not affect the other, giving the desired results.

  • XascoriaXascoria Posts: 3Member

    Thanks a lot for the response Twisted, doing that solved the problem!

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin
    edited June 2020

    @Xascoria said:
    Thanks a lot for the response Twisted, doing that solved the problem!

    Awesome! I'm glad that was able to fix it. :+1:

    (Edit: Changed discussion to a question and marked the reply as the answer)

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.