Character doesn't move when used state machine

Godot Version

4.7.2

Question

I used state machine for the player but it wouldnt move also used var direction but still it wouldn’t move




 

#anim tree
func ready() -> void:
	anim_tree.active = true
	
	

func update_animation():
	anim_tree.set("parameters/BlendSpace1D/blend_position", direction.x)



func _physics_process(delta: float) -> void:
	_apply_gravity(delta)
	_process_state(delta)









func _apply_gravity(_delta: float) -> void:
	if not is_on_floor():
		velocity.y  += gravity * _delta
		
func _process_state(_delta: float) -> void:
	
	match State:
		State.idle: _state_idle()
		State.walk: _state_walk()
		State.jump: _state_jump()
		State.fall: _state_fall()
		State.land: _state_land(_delta)
		State.death: _state_death(_delta)
		
		
	
		
		
		


func _state_idle() -> void:	
	velocity.x = move_toward(velocity.x, 0.0, speed)

	
	#state change
	var _direction: float = Input.get_axis("ui_left", "ui_right")
	if _direction != 0.0:
		_change_state(State.walk)
		
	elif Input.is_action_just_pressed("jump") and is_on_floor():
		_change_state(State.jump)
	elif not is_on_floor():
		_change_state(State.fall)
		
func _state_walk() -> void:
	var _direction: float = Input.get_axis("ui_left", "ui_right")
	velocity.x = _direction * speed 



	if _direction < 0.0:
		bodyvis.scale.x = -1.0
	elif _direction > 0.0:
		bodyvis.scale.x = 1.0

	#transitions
	if _direction == 0.0:
		_change_state(State.idle)
	elif  Input.is_action_just_pressed("jump") and is_on_floor():
		_change_state(State.jump)
	elif not is_on_floor():
		_change_state(State.fall)

func _state_jump() -> void:
	velocity.y = jump_velocity
	_change_state(State.fall)

func _state_fall() -> void:
	var _direction: float = Input.get_axis("ui_left", "ui_right")
	velocity.x = _direction * speed
	
	
	#sprite flip
	if _direction < 0.0:
		bodyvis.scale.x = -1.0
	elif _direction > 0.0:
		bodyvis.scale.x = 1.0
	
		
	if is_on_floor():
		_change_state(State.land)
		
		
		
func _state_land(delta: float) -> void:
	velocity.x = move_toward(velocity.x, 0.0, speed * 3.0)
	land_timer -= delta
	
	if land_timer <= 0.0:
		var _direction: float = Input.get_axis("ui_left", "ui_right")
		if _direction != 0.0:
			_change_state(State.walk)
		else:
			_change_state(State.idle)

func _state_death(delta: float) -> void:
	velocity.x = move_toward(velocity.x, 0.0, speed * 3.0)
	death_timer -= delta
	
	
	if death_timer <= 0.0:
		global_position = respawn_pos
		velocity = Vector2.ZERO
		_change_state(State.idle)

#helpers
func _change_state(new_state: State) -> void:
	#don't reenter the same state
	if new_state == current_State:
		return
	
	
	
	#entry actions
	match new_state:
		State.land:
			land_timer = land_duration
		State.death:
			death_timer = death_duration
		
	

You should match a variable holding the current state, not the State enum/class.

wheres the rest of the code

I see lots of velocity and direction and gravity calculations but for a CharacterBody2D I would also expect one of the move commands such as move_and_collide() or move_and_slide(). Without one of these your character probably isn’t going anywhere.

I’m not sure what you are asking? I just quoted the part from your script I was commenting on, so you can see which exact line I was writing about.