My Problem

I am currently building a platformer game. I was attempting to use move_and_slide_with_snap for the first time and can't find it working for me. My character does snap to the ground like I expected, but I find that it still slides when it's on a slope. I think that I implemented the code properly with it.

If there is a solution I would love to hear it. What am I doing wrong?

func _physics_process(delta):

var current_gravity = GRAVITY - (int(is_on_wall() and inventory.Frog) * GRAVITY/2)
var current_jump_force = JUMP_FORCE

var x_input = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")

if x_input != 0:

    motion.x += x_input * ACCELERATION * delta
    motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)



if !is_on_wall():
    if motion.y <= max_fall_speed:
        motion.y += current_gravity * delta
    if motion.y <= max_slip_speed:
        motion.y += current_gravity * delta

if is_on_floor() or is_on_wall():
    if x_input == 0:
        motion.x = lerp(motion.x, 0, FRICTION)
    if Input.is_action_just_pressed("ui_up"):
        var can_jump = int(inventory.Spring)
        motion.y = -(current_jump_force + (JUMP_FORCE / 2.5 * can_jump))
    if Input.is_action_just_released("ui_up") and motion.y < -JUMP_FORCE/2:
        motion.y = -current_jump_force/2
    if x_input == 0:
        motion.x = lerp(motion.x, 0, AIR_RESISTANCE)

if !motion.y:
    motion = move_and_slide_with_snap(motion, Vector2.DOWN * 320, Vector2.UP, true, 1)
    motion = move_and_slide(motion, Vector2.UP)

I struggled with this for a long time. For me, the solution was centered around having a correct snap vector. Using get_floor_normal() as snap when not jumping resolved my problem. For example:

snap = get_floor_normal()
if is_on_floor():
  if Input.is_action_just_pressed("jump"):
    snap = Vector3.ZERO

move_and_slide_with_snap(velocity, snap, Vector3.UP, true, 4, max_slope_angle)

I took this example code from my own project which is why I have Vector3's. I believe it will work just the same for Vector2. Let me know if this solves the problem for you.

  • 1
    \$\begingroup\$ You may also need to clamp your velocity. More information can be found here \$\endgroup\$
    – rawburt
    Oct 7 '20 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.