Godot Version 4.5.1
Question
Ok so when I release my boost action it, for reasons I don’t understand, won’t trigger my deceleration code. It will decelerate when I run off a cliff and trigger the fall state, but then when I hit the ground the player will be stuck in the last velocity they had when falling. Also for some reason it prints boosting when I hold the jump button while I’m jumping or falling. I don’t even begin to know what that’s about.
Here’s my code (anything else to help clean this up would be appreciated!):
extends CharacterBody2D
var weight = 5
var JUMP = -3000
var speed = 500
var maxspeed = 1500
var boostmax = 100000
var decel = 50
var BOOST
@onready var sprite: Sprite2D = $Sprite2D
@onready var collbox: CollisionShape2D = $CollisionShape2D
enum States {idle, jump, fall, walk, boost, dive, decelerate, landing}
var State = States
func _ready() -> void:
State = States.idle
BOOST = false
func _unhandled_input(InputEvent) -> void:
#jump
if Input.is_action_just_pressed("Jump") and is_on_floor():
#change state to jump
State = States.jump
#jump increases with velocity.x
if velocity.x > 0:
velocity.y += JUMP - velocity.x/4
elif velocity.x < 0:
velocity.y += JUMP + velocity.x/4
else:
velocity.y += JUMP
#variable jump height
if Input.is_action_just_released("Jump") and velocity.y < 0:
velocity.y = JUMP/4.0
if Input.is_action_pressed("Boost"):
BOOST = true
if Input.is_action_just_released("Boost"):
BOOST = false
#move right
if Input.is_action_pressed("Right") and BOOST == false and velocity.x <= maxspeed:
velocity.x += speed
State = States.walk
#boost right
elif Input.is_action_pressed("Right") and BOOST == true and velocity.x <= boostmax:
velocity.x += speed
State = States.boost
#move left
if Input.is_action_pressed("Left") and BOOST == false and velocity.x >= -maxspeed:
velocity.x -= speed
State = States.walk
#boost left
elif Input.is_action_pressed("Left") and BOOST == true and velocity.x >= -boostmax:
velocity.x -= speed
State = States.boost
##doesn't decelerate or leave the boost state when release the boost button
#decelerate right
if Input.is_action_just_released("Right") and BOOST == false:
State = States.decelerate
#decelerate left
if Input.is_action_just_released("Left") and BOOST == false:
State = States.decelerate
#dive
if Input.is_action_just_pressed("Down") and not is_on_floor():
velocity.y += -JUMP * weight
State = States.dive
func _physics_process(delta: float) -> void:
#Add the gravity.
velocity += get_gravity() * delta * weight
#flip sprite
if velocity.x < 0:
sprite.flip_h = true
#animated_sprite.flip_h = true
if velocity.x > 0:
sprite.flip_h = false
#animated_sprite.flip_h = false
#idle state
if State == States.idle:
print("idle")
if not is_on_floor() and velocity.y > 0:
State = States.fall
#jump state
if State == States.jump:
print("jump")
if velocity.y >= 0:
State = States.fall
if BOOST == false:
if velocity.x > 0:
velocity.x -= decel
elif velocity.x < 0:
velocity.x += decel
elif velocity.x == 0:
State = States.idle
#fall state
if State == States.fall:
print("fall")
if is_on_floor():
State = States.landing
if BOOST == false:
if velocity.x > 0:
velocity.x -= decel
elif velocity.x < 0:
velocity.x += decel
elif velocity.x == 0:
State = States.idle
if State == States.landing:
print("landed")
if velocity.x == 0:
State = States.idle
if velocity.x != 0 and BOOST == false:
State = States.walk
if velocity.x != 0 and BOOST == true:
State = States.boost
#walk state
if State == States.walk:
print("walking")
if not is_on_floor() and velocity.y >= 0:
State = States.fall
elif not is_on_floor() and velocity.y < 0:
State = States.jump
#boost state
##fix how overlaps with fall state while holding jump. seems like it overlaps with jump state too toward the apex of the jump
if State == States.boost:
print("boosting")
if not is_on_floor() and velocity.y >= 0:
State = States.fall
elif not is_on_floor() and velocity.y < 0:
State = States.jump
#dive state
if State == States.dive:
print("dive")
if is_on_floor():
State = States.landing
if State == States.decelerate:
print("decelerating")
if velocity.x > 0:
velocity.x -= decel
elif velocity.x < 0:
velocity.x += decel
elif velocity.x == 0:
State = States.idle
print(velocity.x)
move_and_slide()