Attention | Topic was automatically imported from the old Question2Answer platform. | |
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!