My character in Godot moves only LEFT and RIGHT

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

Hello, everybody. I am following a youtube tutorial while learning Godot 3.5. But I have a problem. My character moves only left or right, even when I push up and down arrows, so it´s not like the game doesn´t response to up and down key pressed. It does, but it executes wrong directions.

I checked if I forgot to change the code with up/down/left/right directions after I copy pasted it (because I first wrote code for right and then copied it and just changed directions) and I also checked if the indention is correct. But that´s not the problem, so it seems. I am new to Godot and appreciate any advice. Here is my code, which is split into several scripts and they are connected to each other by using extends…:

Script for “Idle”:

extends State

class_name IdleState

func move_left():
	change_state.call_func("run")

func move_right():
	change_state.call_func("run")

func move_up():
	change_state.call_func("run")

func move_down():
	change_state.call_func("run")

Script for “Player”:

extends KinematicBody2D

class_name PersistentState

var state
var state_manager

var velocity = Vector2()
var speed = 100

func _ready():
	state_manager = StateManager.new()
	change_state("idle")
	
func get_input():
	velocity = Vector2()
	if Input.is_action_pressed("ui_left"):
		move_left()
	if Input.is_action_pressed("ui_right"):
		move_right()
	if Input.is_action_pressed("ui_up"):
		move_up()
	if Input.is_action_pressed("ui_down"):
		move_down()
	velocity = velocity.normalized()*speed
func _process(delta):
	get_input()
	velocity = move_and_slide(velocity)
	print(state)

func move_left():
	state.move_left()

func move_right():
	state.move_right()

func move_up():
	state.move_up()

func move_down():
	state.move_down()
	
func change_state(new_state_name):
	if state != null:
		state.queue_free()
	state = state_manager.get_state(new_state_name).new()
	state.setup(funcref(self, "change_state"), $AnimationPlayer, self)
	state.name = str(new_state_name)
	add_child(state)

Script for “Run”:

extends State

class_name RunState

func get_input():
	if Input.is_action_just_released("ui_left"):
		change_state.call_func("idle")
	if Input.is_action_just_released("ui_right"):
		change_state.call_func("idle")
	if Input.is_action_just_released("ui_up"):
		change_state.call_func("idle")
	if Input.is_action_just_released("ui_down"):
		change_state.call_func("idle")

func _physics_process(delta):
	get_input()
	
func move_left():
	persistent_state.velocity.x -= 1

func move_right():
	persistent_state.velocity.x += 1
	
func move_down():
	persistent_state.velocity.x -= 1

func move_up():
	persistent_state.velocity.x += 1

Script for “Stagemanager”:

class_name StateManager

var states

func _init():
	states = {
		"idle": IdleState,
		"run": RunState,
	}

func get_state(state_name):
	if states.has(state_name):
		return states.get(state_name)
	else:
		printerr("No State ", state_name, "Exists")

Script for “State”:

extends Node2D

class_name State

var change_state
var animation
var persistent_state
var velocity = 0

func _physics_process(delta):
	persistent_state.move_and_slide(persistent_state.velocity, Vector2.UP)

func setup(change_state, animation, persistent_state):
	self.change_state = change_state
	self.animation = animation
	self.persistent_state = persistent_state
:bust_in_silhouette: Reply From: Suleymanov
  func move_down():
     persistent_state.velocity.x -= 1

  func move_up():
     persistent_state.velocity.x += 1

Change “x” to “y”

Oh, thank you. It was so simple, that now I feel dumb. That´s a price to pay for copy pasting previous lines of code. I seriously consider that for a beginner like me it might be more beneficial to write everything manually until it becomes second nature before using shortcuts to do things.

I also had to change -= 1 into += 1 in move_down and +=1 into -=1 in move_up. Or else the up would trigger down and down would trigger up. Thanks again.

MirkaPavlinska | 2023-06-19 18:36

If you feel dumb, then welcome to the club, we all do :slight_smile: Keep up creating great stuff!

Suleymanov | 2023-06-20 06:23