0 votes
extends KinematicBody2D

enum {
    IDLE,
    RUN,
    JUMP
}

var gravity = 10
var speed = 10000
var jump = 200
var state = RUN
var motion = Vector2.ZERO
var x_input

func _physics_process(delta):
    x_input = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
    motion.y += gravity

    match state:
        IDLE:
            print("idling")
            inputstuff()
        RUN:
            print("running")
            move(delta)
        JUMP:
            print("jump")
            _jump()

    motion = move_and_slide(motion, Vector2.UP)

func inputstuff():
    if x_input != 0:
        state = RUN
    if Input.is_action_pressed("ui_up"):
        state = JUMP

func move(delta):
    if x_input != 0:
        motion.x = x_input * speed * delta
    else:
        state = IDLE
    if Input.is_action_pressed("ui_up"):
        state = JUMP

func _jump():
    if is_on_floor():
        if Input.is_action_pressed("ui_up"):
            motion.y = -jump
    if x_input != 0:
        state = RUN
    else:
        state = IDLE

this is the first time I made a state machine and it looks garbage but it actually works. I watched a bunch of tutorials but It's just hard to understand since I just started around a week ago, the one made by heartbeast was the only one I understood. Any recommendations on getting better at this.

asked Mar 25 in Engine by Sininenblue (26 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.