Godot Version
4.6.3
Question
so I was watching a video called “2D Walk, Run, Crawl Movement System | Godot 4.6” so I could optimize my movement and animation code and once I reached func frame_animation() -> String: Godot gave me an error saying “Not all code paths return a value.” it persisted on even after typing the rest of the code. (I used idle for most of the animation code because I don’t have any animation for them yet.)
This is my code:
extends CharacterBody2D
var gravity: float = 16.5
var jump_height: float = -185.0
func _physics_process(_delta: float) -> void:
var input_x = Input.get_axis("left", "right")
velocity.x = input_x * get_player_speed()
if !is_on_floor() and Input.is_action_just_pressed("jump"):
velocity.y = jump_height
velocity.y += gravity
move_and_slide()
$Sprite2D/AnimationPlayer.play(frame_animation())
func get_player_speed() -> float:
if Input.is_action_pressed("shift"):
return 85.0 # running
return 20.0 # walking
func frame_animation() -> String:
var is_moving: bool = velocity.x != 0
if is_moving:
$Sprite2D.flip_h = velocity.x < 0
if !is_on_floor():
return "idle" if velocity.y < 0 else "idle"
if is_moving:
if Input.is_action_pressed("shift"):
return "idle"
return "walk"