Strugling to make my character move

Godot Version

Godot 4.5

Question

can’t make my character move or receive calls to change her sprite, i really have no idea what i’m doing wrong

extends CharacterBody2D

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

var cordinal_direction : Vector2 = Vector2.DOWN
var direction : Vector2 = Vector2.ZERO
var walk_speed : float = 20
var state : String = "(idle)"


func _ready() -> void:
	pass


func _process( delta ):
		
	
	velocity.y = Input.get_action_strength("up") - Input.get_action_strength("down")
	velocity.x = Input.get_action_strength("left") - Input.get_action_strength("right")
	
	velocity += direction * walk_speed * delta
	
			
	
	
		
	if SetState() == true:
		UpdateAnimation()
		
func _physics_process( delta ):
		move_and_slide()
		




func SetDirection() -> bool:
	
	return true

func SetState() -> bool:
	var new_state : String = "(idle)" if direction == Vector2.ZERO else "(walk)"
	if state == new_state:
		return false
	state = new_state
	return true
func UpdateAnimation() -> void:
	animated_sprite_2d.play(state + AnimationDirection())

func AnimationDirection() -> String:
	if cordinal_direction == Vector2.DOWN:
		return "back"
		
	elif cordinal_direction == Vector2.UP:
		return "front"
		
	elif cordinal_direction == Vector2.LEFT:
		return "side-left"
		
	else:
		return "side-right"
	
		

This code is your problem. You are directly assigning values to velocity, then replacing those values with direction which never changes, and therefore is always zero. Whatever you multiply by zero is also zero, which is why your character never moves.

Try this code instead:

direction = Input.get_vector("left", "right", "up", "down")

velocity = direction * walk_speed * delta

Then let’s talk about this code.

func AnimationDirection() -> String:
	if cordinal_direction == Vector2.DOWN:
		return "back"
		
	elif cordinal_direction == Vector2.UP:
		return "front"
		
	elif cordinal_direction == Vector2.LEFT:
		return "side-left"
		
	else:
		return "side-right"

First, function names should be snake_case, and prepended with an underscore if they are private (only used by this class). PascalCase indicates a class name.

Also, cordinal_direction does not change. Also, “cordinal” is not a word. Perhaps you meant “cardnial”? Which means, north, south, east, west. At any rate, you don’t need this direction. Instead, try this:

func _animation_direction() -> String:
	if direction.y < 0:
		return "back"
	elif direction.y >= 0:
		return "front"
	elif direction.x < 0:
		return "side-left"
	else:
		return "side-right"

Then the whole thing becomes:

extends CharacterBody2D

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

var direction : Vector2 = Vector2.ZERO
var walk_speed : float = 20
var state : String = "(idle)"


func _physics_process( delta ):
	direction = Input.get_vector("left", "right", "up", "down")

	if _set_state() == true:
		_update_animation()

	velocity = direction * walk_speed * delta

	move_and_slide()


func _set_state() -> bool:
	var new_state : String = "(idle)" if direction == Vector2.ZERO else "(walk)"
	if state == new_state:
		return false
	state = new_state
	return true


func _update_animation() -> void:
	animated_sprite_2d.play(state + _animation_direction())


func _animation_direction() -> String:
	if direction.y < 0:
		return "back"
	elif direction.y >= 0:
		return "front"
	elif direction.x < 0:
		return "side-left"
	else:
		return "side-right"

A few other things I changed were getting rid of a function that did nothing and wasn’t used, getting rid of extra whitespace, and combining the _process() and _physics_process() functions.

i tryed this code too but it still doesnt work,trying to verify if its some node i set up wrong or something like that

ye it was something on my end,now it works thanks!

1 Like