KinematicBody2D problem when using velocity.x

jsenajsena Posts: 7Member

I'm making a platformer game and I'm using the KinematicBody2D demo with some changes. For the rolling I'm applying some force using velocity.x = ROLL_FORCE but when the player tries to roll into a slope the player jumps instead of just moving foward (like the image below).

I'm getting an B instead of an A
How can I solve this?

Best Answer

  • SomnivoreSomnivore Posts: 102
    Accepted Answer

    @jsena said:
    Thanks for the answer. I understand what you said but I can't imagine how to translate it to code :(

    Well, you know you need a raycast, so you would want to add one to whatever your object is. From there you would grab your raycast node from your object script via get_node(). In fixed_process() you can query the raycast node to see if it's colliding with anything, and grab a normal from the collision too. The normal is a Vector2 which determines the way the surface is pointing; you would normal.slide(velocity) to move along it.

    https://godot.readthedocs.io/en/stable/classes/class_raycast.html

Answers

  • SomnivoreSomnivore Posts: 102Member

    Image B depicts a ball, or a barrel, going over a slope; it launches because its y velocity is negative, a product of sliding its forward momentum against a diagonal slope (i.e. (10, 0) might become (7, -3) after sliding); with nothing but gravity to push it back to the earth, once the slope ends, it'll continue to progress upwards until gravity can make y positive again, hence you launch. Image A, however, depicts a train, or a minecart, hooked on a rail; it cannot leave that rail unless it reaches its end, because its velocity is always sliding along the surface normal whether or not the train or cart has become temporarily launched into the air.

    The rail is a raycast fired down from your character into the earth which detects the normal you either are or were traveling along; if your character is not detecting collisions, and its y-velocity is negative but your rail is recognizing ground, that means your character has been launched; in this case you just need to slide your character's velocity along the normal detected by your rail and you should travel along that slope's angle despite not colliding with it. Gravity should handle the rest of the problem.

  • jsenajsena Posts: 7Member

    @Somnivore said:
    [...]
    The rail is a raycast fired down from your character into the earth which detects the normal you either are or were traveling along; if your character is not detecting collisions, and its y-velocity is negative but your rail is recognizing ground, that means your character has been launched; in this case you just need to slide your character's velocity along the normal detected by your rail and you should travel along that slope's angle despite not colliding with it. Gravity should handle the rest of the problem.

    Thanks for the answer. I understand what you said but I can't imagine how to translate it to code :(

  • SomnivoreSomnivore Posts: 102Member
    Accepted Answer

    @jsena said:
    Thanks for the answer. I understand what you said but I can't imagine how to translate it to code :(

    Well, you know you need a raycast, so you would want to add one to whatever your object is. From there you would grab your raycast node from your object script via get_node(). In fixed_process() you can query the raycast node to see if it's colliding with anything, and grab a normal from the collision too. The normal is a Vector2 which determines the way the surface is pointing; you would normal.slide(velocity) to move along it.

    https://godot.readthedocs.io/en/stable/classes/class_raycast.html

Sign In or Register to comment.