My character wont stop

Godot Version

4.2.2

Question

Hello! Im not very experienced in programing yet and ivebeen all night trying to make a top down movement, but simply wont work, im so close to make it funcional but now my problem is that the character simply wont stop walking.
pls help u_u

heres my code

extends CharacterBody2D

var speed = 150
var run_speed = 230
var is_running = false
var is_moving = false
@onready var animation = $AnimatedSprite2D

func _ready():
	pass

func _physics_process(delta):
	
	# Movimiento
	if Input.is_action_pressed("move_right"):
		velocity.x += 1
	if Input.is_action_pressed("move_left"):
		velocity.x -= 1
	if Input.is_action_pressed("move_down"):
		velocity.y += 1
	if Input.is_action_pressed("move_up"):
		velocity.y -= 1
	
	is_running = Input.is_action_pressed("ui_back")
	
	var current_speed = speed
	if is_running:
		current_speed = run_speed
	
	if velocity != Vector2.ZERO:
		velocity = velocity.normalized() * current_speed
		move_and_slide()
		
		if abs(velocity.x) > abs(velocity.y):
			if velocity.x > 0:
				if animation.animation != "walk_right":
					animation.play("walk_right")
			else:
				if animation.animation != "walk_left":
					animation.play("walk_left")
		else:
			if velocity.y > 0:
				if animation.animation != "walk_down":
					animation.play("walk_down")
			else:
				if animation.animation != "walk_up":
					animation.play("walk_up")
	elif Input.is_action_just_released("move_down"):
		animation.play("default_down")
	elif Input.is_action_just_released("move_up"):
		animation.play("default_up")
	elif Input.is_action_just_released("move_right"):
		animation.play("default_right")
	elif Input.is_action_just_released("move_left"):
		animation.play("default_left")
		animation.stop()

reset velocity.x and velocity.y back to 0 at begin of the _physics_process function before doing anything else

Hey there! Change it to:

velocity = Input.get_axis("move_left", "move_right", "move_up", "move_down")
1 Like

Thank You, this was so effective!!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.