I'm new to Godot so please bear with me.
I'm trying to make a basic platforming script.
My issue is that the is_on_floor()
is returning false every other frame while moving on the ground (including acceleration and decelerating) but not while standing still. This makes the player jitter up and down sort of, and messes up my movement speed.
var state = NAN
enum {
GROUND,
AIR }
const MAX_SPEED = 80
const ACCELERATION = 7
const GROUND_FRICTION = 10
const JUMP_FORCE = 180
const AIR_MAX_SPEED = 40
var GRAVITY = 5
var motion = Vector2.ZERO
var x_input = 0
var jump_input = 0
func _physics_process(delta):
_handle_input()
_handle_state()
if state == GROUND:
_ground_movement(delta)
elif state == AIR:
_air_movement(delta)
_move()
func _handle_input():
x_input = -Input.get_action_strength("ui_left") + Input.get_action_strength("ui_right")
jump_input = Input.get_action_strength("ui_up")
func _handle_state():
if is_on_floor():
state = GROUND
else:
state = AIR
func _ground_movement(delta):
if x_input != 0:
motion.x += x_input * ACCELERATION
motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
else:
motion.x = lerp(motion.x, 0, GROUND_FRICTION * delta)
if jump_input != 0:
motion.y -= JUMP_FORCE
else:
motion.y = 0
func _air_movement(_delta):
if x_input != 0:
motion.x += x_input * ACCELERATION
motion.x = clamp(motion.x, -AIR_MAX_SPEED, AIR_MAX_SPEED)
motion.y += GRAVITY
func _move():
#print(state)
motion = move_and_slide(motion, Vector2.UP)
So for example if I print the state every frame i get this output:
0
1
0
1
0
etc.
Any help appreciated, also feel free to point out if you think I should be doing things differently in the rest of the code, as I said I'm new.