Hi, I'm making a little platformer prototype, and I'm using the 'moveandslidewithsnap()' 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():
#jump
if is_on_floor():
if Input.is_action_just_pressed("jump"):
velocity.y = -jump_force
else:
if Input.is_action_just_released("jump"):
if velocity.y < jump_stop:
velocity.y = jump_stop
#gravity
if not is_on_floor():
velocity.y += gravity
func _physics_process(delta):
_input_loop()
_movement_loop(input_direction)
_v_movement()
velocity = move_and_slide_with_snap(velocity, Vector2.DOWN * 10, Vector2.UP)