Attention | Topic was automatically imported from the old Question2Answer platform. | |
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():
#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)
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