Unintentional jump

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

Hi, I’m making a little platformer prototype, and I’m using the ‘move_and_slide_with_snap()’ function, and it’s doing exactly what I want, but when going down a slope, sometimes it decides to stop snapping, and jump up into the air. I’m not sure what’s the issue. Here’s my code :

extends KinematicBody2D

var max_speed : float = 500
var acceleration : float = 10
var gravity : float = 20

var jump_force : float = 600
var jump_stop : float = 10

#this acts as a one dimentional vector
var input_direction : float
var velocity : Vector2

func _input_loop():

	var move_left = Input.get_action_strength("move_left")
	var move_right = Input.get_action_strength("move_right")

	input_direction = move_right - move_left

func _movement_loop(i_dir):

	velocity.x = move_toward(velocity.x, i_dir * max_speed, acceleration)

func _v_movement():

	if is_on_floor():
		if Input.is_action_just_pressed("jump"):
			velocity.y = -jump_force
		if Input.is_action_just_released("jump"):
			if velocity.y < jump_stop:
				velocity.y = jump_stop

	if not is_on_floor():
		velocity.y += gravity

func _physics_process(delta):
	velocity = move_and_slide_with_snap(velocity, Vector2.DOWN * 10, Vector2.UP)

Tried it and noticed the same. It occurs at corners where slope steepens. I think the collider hits the corner while already falling, thus changing the movement vector. I’m not sure how to fix it though. Maybe somebody who has more experience with platformer controllers can?

I also tried with 3.4 beta as they made some improvements to move_and_slide functions, but no difference in this case.

aXu_AP | 2021-10-18 08:37

:bust_in_silhouette: Reply From: aXu_AP

It seems that the snap vector isn’t big enough. When I set it to 10 times more, the problem didn’t occur anymore in my test level. As a side effect, jumping also didn’t work, but that was easy to work around by removing the snap vector upon jumping.

var snap_vector = Vector2.DOWN * 100 if not Input.is_action_just_pressed("jump") else Vector2.ZERO
velocity = move_and_slide_with_snap(velocity, snap_vector, Vector2.UP)

You may need to further tweak it for your use case.

Hi, I was trying to implement your solution, and it seemed to be working, however when the character goes over a cliff, I want him to fly off without snapping down to the bottom, and this is the case, however he still jumps.

sketchysquirrel | 2021-10-18 22:19

Have you tried different values? Maybe 100 is an overkill, try 50 next.

aXu_AP | 2021-10-19 02:59

Yes I did, and I do think that 100 is overkill, because If the character walks off a clif, and the floor is within 100px of the character, then it snaps all the way down, however if it isn’t and the character flys off the edge, it leaps up into the air. I tried to do a manual solution using a raycast, and a collision shape that only covers the top section of the character, however it’s a bit buggy, and it doesn’t work with moving platforms (which the move_and_slide_with_snapping() function fixes) so I’m just a bit lost. :frowning:

sketchysquirrel | 2021-10-19 23:49