Looking at the code posted, something like this should hopefully work, so long as the ground raycast can reach the ground and is setup correctly:
extends KinematicBody2D
const UP = Vector2(0, -1)
var velocity = Vector2()
var move_speed = 32 * 8
var gravity = 600
var jump_velocity = -250
# Track whether the jump action is down with this variable:
var input_jump_down = false
var speed_y = 0
var is_grounded = false
onready var ground_ray = get_node("ground_ray")
func _ready():
pass
func _physics_process(delta):
_update_grounded()
velocity.y += gravity * delta
_get_input()
if (input_jump_down == true and is_grounded = true):
velocity.y = jump_velocity
is_grounded = false
velocity = move_and_slide_with_snap(velocity, UP)
_flip()
func _update_grounded():
# Make sure we have the latest raycast data
ground_ray.force_raycast_update()
# Update the is_grounded variable
is_grounded = ground_ray.is_colliding()
func _get_input():
var move_direction = -int(Input.is_action_pressed("moveleft")) + int(Input.is_action_pressed("moveright"))
velocity.x = lerp(velocity.x, move_direction * move_speed, 0.2)
if (Input.is_action_pressed("jump")):
input_jump_down = true
else:
input_jump_down = false
func _flip():
if Input.is_action_pressed("moveleft"):
get_node("Body/playerSprite").scale.x = -1
elif Input.is_action_pressed("moveright"):
get_node("Body/playerSprite").scale.x = 1
Hopefully this works and helps! :smile:
The only really big change I made is moving checking if the ground_ray is colliding to _physics_process so it is in sync with the physics engine, and move jump detection to _get_input
instead of _input
.
(Side note: I edited your post so the code is formatted correctly)