Godot Version
4.5.1
Question
why is the is_on_floor not working
extends CharacterBody2D
@export var SPEED = 150
@export_range(0,1) var deceleration = 0.1
@export_range(0,1) var acceleration = 0.1
const jump_force = -600.0
const Jump_deceleration = 1500.0
const fall_gravity:= 1500.0
const fall_velocity := 500.0
const wall_slide_gravity:= 300.0
const Wall_slide_velocity:= 500.0
const Wall_jump_length := 30.0
const Wall_jump_velocity := -500.0
const Dash_length := 200
const Dash_velocity := 600
enum State {
FAll,
FLOOR,
JUMP,
Wall_slide,
Wall_jump,
Dash,
}
@onready var animated_sprite_2d: AnimatedSprite2D = %AnimatedSprite2D
@onready var coytote_timer: Timer = %coytoteTimer
@onready var ledge_climbray_cast: RayCast2D = %LedgeClimbrayCast
@onready var ledgespaceraycast: RayCast2D = %ledgespaceraycast
@onready var player_collider: CollisionShape2D = %PlayerCollider
@onready var ray_cast_2d: RayCast2D = %RayCast2D
@onready var dash_cooldown: Timer = %"dash cooldown"
func _physics_process(delta: float) -> void:
process_state(delta)
move_and_slide()
var active_state := State.FAll
var facing_direction := 1.0
var saved_position := Vector2.ZERO
var can_dash = false
var dash_jump_buffer := false
func _ready() -> void:
switch_state(active_state)
func switch_state(to_state: State) -> void:
var previous_state := active_state
active_state = to_state
match active_state:
State.FLOOR:
can_dash = true
State.FAll:
animated_sprite_2d.play("fall")
if previous_state == State.FLOOR:
coytote_timer.start()
State.JUMP:
animated_sprite_2d.play("jump")
velocity.y = jump_force
coytote_timer.stop()
State.Wall_slide:
animated_sprite_2d.play("cliumb")
velocity.y = 0
can_dash =true
State.Wall_jump:
animated_sprite_2d.play("jump")
velocity.y = Wall_jump_velocity
set_facing_direction(-facing_direction)
saved_position = position
State.Dash:
if dash_cooldown.time_left > 0:
active_state = previous_state
return
animated_sprite_2d.play("dash")
velocity.y = 0
set_facing_direction(signf(Input.get_axis("left","right")))
velocity.x = facing_direction * Dash_velocity
saved_position = position
can_dash = previous_state == State.FLOOR or previous_state == State.Wall_slide
dash_jump_buffer = false
func process_state(delta: float) -> void:
match active_state:
State.FAll:
velocity.y = move_toward(velocity.y, fall_velocity, fall_gravity * delta)
handle_movement()
if is_on_floor():
switch_state(State.FLOOR)
elif is_input_toward_facing() and can_wall_slide():
switch_state(State.Wall_slide)
elif Input.is_action_just_pressed("dash") and can_dash:
switch_state(State.Dash)
State.FLOOR:
if Input.get_axis("move_left", "move_right"):
animated_sprite_2d.play("walk")
else:
animated_sprite_2d.play("idle")
handle_movement()
if not is_on_floor():
switch_state(State.FAll)
elif Input.is_action_just_pressed("jump"):
switch_state(State.JUMP)
elif Input.is_action_just_pressed("dash"):
switch_state(State.Dash)
State.JUMP,State.Wall_jump:
velocity.y = move_toward(velocity.y, 0, Jump_deceleration * delta)
if active_state == State.Wall_jump:
var distance := absf(position.x - saved_position.x)
if distance >= Wall_jump_length or can_wall_slide():
active_state= State.JUMP
else:
handle_movement(facing_direction)
if active_state != State.Wall_jump:
handle_movement()
if Input.is_action_just_pressed("jump") or velocity.y >= 0:
velocity.y=0
switch_state(State.FAll)
elif Input.is_action_just_pressed("dash") and can_dash:
switch_state(State.Dash)
State.Wall_slide:
velocity.y= move_toward(velocity.y, Wall_slide_velocity, wall_slide_gravity * delta)
handle_movement()
if is_on_floor():
switch_state(State.FLOOR)
elif not can_wall_slide():
switch_state(State.FAll)
elif Input.is_action_just_pressed("jump"):
switch_state(State.Wall_jump)
elif Input.is_action_just_pressed("dash"):
set_facing_direction(-facing_direction)
switch_state(State.Dash)
State.Dash:
dash_cooldown.start()
if is_on_floor():
coytote_timer.start()
if Input.is_action_just_pressed("jump"):
dash_jump_buffer = true
var distance := absf(position.x - saved_position.x)
if distance >= Dash_length or signf(get_last_motion().x) != facing_direction:
if dash_jump_buffer and coytote_timer.time_left > 0:
switch_state(State.JUMP)
elif is_on_floor():
switch_state(State.FLOOR)
else:
switch_state(State.FAll)
elif can_wall_slide():
switch_state(State.Wall_slide)
func handle_movement(input_direction: float = 0) -> void:
if input_direction ==0:
input_direction = signf(Input.get_axis("left","right"))
set_facing_direction(input_direction)
func is_input_toward_facing() -> bool:
return signf(Input.get_axis("left","right")) ==facing_direction
func can_wall_slide() -> bool:
return is_on_wall_only() and ray_cast_2d.is_colliding()
func set_facing_direction(direction: float) -> void:
if direction:
animated_sprite_2d.flip_h = direction < 0
facing_direction=direction
ray_cast_2d.position.x=direction * absf(ray_cast_2d.position.x)
ray_cast_2d.target_position.x = direction *absf(ray_cast_2d.target_position.x)
ray_cast_2d.force_raycast_update()
velocity.x = direction * SPEED