Mycharacter is not moving

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ExpNeeded
extends CharacterBody2D

var SPEED = Vector2.ZERO
var STATE = IDLE

enum { IDLE, MOVE, JUMP, CLIMB, COMBAT }

func _physics_process(delta):
	moveSTATE(delta)
	jumpSTATE(delta)
	animation()
	
func moveSTATE(delta):
	if Input.is_action_pressed("MoveLeft") or Input.is_key_pressed(KEY_A):
		SPEED.x -= 100 * delta
		STATE = MOVE
		$WarriorSprite.flip_h = true
	elif Input.is_action_pressed("MoveRight") or Input.is_key_pressed(KEY_A):
		SPEED.x += 100 * delta
		STATE = MOVE
		$WarriorSprite.flip_h = false
	else:
		SPEED.x = 0
		STATE = IDLE

func jumpSTATE(delta):
	if Input.is_action_just_pressed("Jump"):
		SPEED.y = -50 * delta
		STATE = JUMP
:bust_in_silhouette: Reply From: a_world_of_madness

You seem to be defining the SPEED variable yourself, and then setting values for it, which is not enough to make a CharacterBody2D move.

The character node has a built in way of keeping track of the velocity of the character, you don’t need to define it yourself. Just modify the velocity property instead, and at the end of the loop, call move_and_slide(), which will then move the node according to the current value of the velocity vector.

You might want to check out the Using CharacterBody2D tutorial on the docs, the last example on the page seems similar to what you are trying to achieve:

extends CharacterBody2D

var speed = 300.0
var jump_speed = -400.0

# Get the gravity from the project settings so you can sync with rigid body nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

 func _physics_process(delta):
     # Add the gravity.
     velocity.y += gravity * delta
 
     # Handle Jump.
     if Input.is_action_just_pressed("jump") and is_on_floor():
        velocity.y = jump_speed
    
     # Get the input direction.
     var direction = Input.get_axis("ui_left", "ui_right")
     velocity.x = direction * speed
    
     move_and_slide()

It works but the anim is weird with the jump

ExpNeeded | 2023-06-11 10:48

Thx for the code btw

ExpNeeded | 2023-06-11 10:56