[Solved] Problem with move_and_slide_with_snap

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By M4dMel

Hello, I just started using Godot with my first attempt at a simple platformer. I’m using the function move_and_slide_with_snap 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:
		ON_FLOOR:
			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()
slopeAccelerationFactor
if floorNormal.x * sign(floorNormal.x) >= 0.5:
	slopeAccelerationFactor = floorNormal.x * sign(floorNormal.x) * 2
else:
	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!

:bust_in_silhouette: Reply From: ClumsyDog

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.

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 :wink:

M4dMel | 2021-02-14 11:37

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

M4dMel | 2021-02-14 17:27

No problem! Glad I could help.

ClumsyDog | 2021-02-15 22:19