+1 vote

Hello, I just started using Godot with my first attempt at a simple platformer. I'm using the function moveandslidewithsnap for movement, which works fine in most cases, but I've noticed something weird: when the player runs up a steep cliff which is also steep on the other side but has a sufficiently narrow horizontal tip, my understanding is that it's supposed to keep sliding down smoothly after passing the edge of the tip, or at least detach itself from the ground and fall. What I observe, however, is that it does a little jump when it reaches the edge, and the steeper the cliff the higher the jump, as if it kept the momentum from the ascending phase and released it when the snapping stops. Whether I'm right or not concerning the cause of the issue, I would really appreciate it if you could give me some advice on how to solve it. Here's the relevant part of my code:

func _physics_process(delta):
    velocity = move_and_slide_with_snap(velocity, snapVector, Vector2.UP, true, 4, 1.22)
    inputVector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
    inputVector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")

    match state:
            if get_slide_count() > 0:
                snapVector = -get_slide_collision(0).normal * 32
                if velocity.x != 0:
                    velocity.y += get_slide_collision(0).normal.y * 32
            if inputVector != Vector2.ZERO:
                velocity.x = move_toward(velocity.x, inputVector.x * MAX_SPEED, FLOOR_ACCELERATION * delta)

I've already tested that the issue remains if I remove the part where I add the normal.y of the slope to the velocity.y. Also the state is always ON_FLOOR until the player jumps, so I don't think there's something weird happening there either. Lastly I apply gravity in the straightforward way on a different class from which the player inherits. Of course if it can help I can post the whole code.
Thank you so much in advance, this is really driving me crazy!

Edit: I also found out that no collision with the floor is detected for a short period of time after the player climbs the slope and proceeds to snap to its horizontal tip. If I make the tip wide enough for the correct collision to be detected before the player reaches its end, the issue is not visible.

Edit 2: for whomever might need this, I found a solution. First of all, I can confirm that my analysis as for the cause of the issue was correct, in that the velocity.y decreases according to gravity from its initial value after the ascending phase to zero, even though it appears to be constantly zero on the tip of the mountain. So what I did was set it to be what I want it to be by hand, that is,

floorNormal = get_floor_normal()
if floorNormal.x * sign(floorNormal.x) >= 0.5:
    slopeAccelerationFactor = floorNormal.x * sign(floorNormal.x) * 2
    slopeAccelerationFactor = 1
if inputVector.x != 0:
    velocity.y = move_toward(velocity.y, floorNormal.x * MAX_SPEED * inputVector.x, FLOOR_ACCELERATION * delta)

Thanks to ClumsyDog for his answer that led me to start from scratch and isolate the problem. Of course if anyone has a different solution I'll gladly listen!

Godot version 3.2.3
in Engine by (13 points)
edited by

1 Answer

0 votes

Pigdev tackled this problem in one of his videos: https://youtu.be/9l-tf97z2bg. He addresses some problems at 10:51 including the problem where you character does a jump (I assume this is what you're talking about, if not, then just watch the whole video. He explains it all there.) I can see you're already passing in a value for the floor max angle however. Maybe tweak the values a bit? Or maybe I didn't understand your problem.

Sadly, I don't know how to solve your problem with the collision detection.

by (112 points)
edited by

Thank you very much for your reply! The video didn't cover my specific problem, but it made me realize that I'm probably doing something wrong, so I'll start over following Pigdev's suggestions. I'll let you know if this eventually leads me to a solution ;)

I edited the post with the solution I found, thanks again!

No problem! Glad I could help.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.