Godot Version
4.6 stable
Help:
ma player dos not jump (i am a bad coder)
i got the input map and it prints jump on state change.
extends CharacterBody2D
enum State {
IDLE,
WALK,
JUMP,
FALL,
#ATACK,
#DASH_ATACK
}
var current_state = State.IDLE
@export var move_speed: int = 180
@export var jump_force: int = 300
@export var dash_force: int = 200
@export var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(delta: float) -> void:
# Aply gravity
if not is_on_floor():
velocity.y += gravity * delta
# Get input
var direction = (+Input.get_action_strength("Right")
-Input.get_action_strength("Left"))
# State Machine
match current_state:
State.IDLE:
velocity.x = 0
if direction != 0:
change_state(State.WALK)
if Input.is_action_just_pressed("Jump") and is_on_floor():
change_state(State.JUMP)
#if Input.is_action_just_pressed("Atack"):
#change_state(State.ATACK)
State.WALK:
velocity.x = direction * move_speed
if direction == 0:
change_state(State.IDLE)
if Input.is_action_just_pressed("Jump") and is_on_floor():
change_state(State.JUMP)
#if Input.is_action_just_pressed("Atack"):
#change_state(State.DASH_ATACK)
State.JUMP:
velocity.x = direction * move_speed
if velocity.y > 0:
change_state(State.FALL)
State.FALL:
velocity.x = direction * move_speed
if is_on_floor():
change_state(State.IDLE)
move_and_slide()
func change_state(new_state: State):
# Exit current state
match current_state:
State.JUMP:
pass # Could stop jump animation
current_state = new_state
# Enter new state
match new_state:
State.JUMP:
print("jump")
velocity.y *= jump_force
animated_sprite.play("jump")
State.IDLE:
print("idle")
animated_sprite.play("idle")
State.WALK:
print("walk")
animated_sprite.play("walk")
State.FALL:
print ("fall")
animated_sprite.play("fall")
Thanks in advice ![]()