Player movement script stopped working out of the blue

Godot Version

4.x

Question

So my movement used to work but one day after opening my project i noticed the player wasn’t moving anymore which could be because of a few changes i made the day before but i don’t remember what i really did because it was really small adjustments to the code. I’m a newbie gamedev and i was trying to make a roguelike similar to vampire survival or brotato.

Also a probably useful info thing while i was trying to debug is i tried printing out the velocity2d (my velocity var) and the position which indicated that the character was moving because in the debug the variables changed depending on my inputs but the sprite did not budge. I also tried to do the same thing on the sprite with a new script and it still said it was moving but nothing changed on the screen.

I tried the same method for the main scene the character was in but this time the position didn’t look like it was changing.

Player Scene Tree

Main Scene Scene Tree

Character Script:

extends CharacterBody2D

@export var speed = 400
@export var health = 10

func _physics_process(delta):
	var velocity2d = Vector2.ZERO
	velocity2d.x = Input.get_axis("left", "right")
	velocity2d.y = Input.get_axis("up", "down")
	
	if velocity2d.length() > 0:
		velocity2d = velocity2d.normalized() * speed
	
	if velocity2d.x != 0:
		if velocity2d.x > 0:
			$Player_Animated_Sprite.flip_h = false
			$Player_Animated_Sprite.play("walk")
		else:
			$Player_Animated_Sprite.flip_h = true
			$Player_Animated_Sprite.play("walk")
	else:
		$Player_Animated_Sprite.play("idle")
	
	global_position += velocity2d * delta
	move_and_slide()
	
	print("plr vel: " + str(velocity2d))
	print("plr pos: " + str(global_position))

Sprite Script:

extends Sprite2D

@export var Bullet : PackedScene

var is_ready: bool = true

func shoot():
	var b = Bullet.instantiate()
	owner.add_child(b)
	b.global_transform = $Muzzle.global_transform
	is_ready = false
	$Timer.start()

func _process(_delta):
	look_at(get_global_mouse_position())
	if get_global_mouse_position().x < get_global_position().x:
		flip_v = true
	else:
		flip_v = false
	
	if Input.is_action_pressed("shoot") and is_ready:
		shoot()
	

func _physics_process(_delta):
	print("gun pos: " + str(global_position))


func _on_timer_timeout():
	is_ready = true

Main Scene Script:

extends Node2D


# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	print("main plr pos: " + str($Player_Scene.position))

Blockquote

Hello, for move_and_slide to actually work, you need to set the velocity attribute of the CharacterBody2D (see CharacterBody2D — Godot Engine (4.2) documentation in English). So, you don’t need to define a new velocity2d variable, the CharacterBody2D already has an attribute for that.
I modified a bit your Player script to take that into account. If you want more details about how to make a CharactersBody2D move, the Godot documentation explains it pretty well here Using CharacterBody2D/3D — Godot Engine (stable) documentation in English.

extends CharacterBody2D

@export var speed = 400
@export var health = 10

func _physics_process(delta):
	var input_dir = Input.get_vector("left", "right", "up", "down")
	velocity = input_dir * speed
	
	if velocity.x != 0:
		$Player_Animated_Sprite.play("walk")
		if velocity.x > 0:
			$Player_Animated_Sprite.flip_h = false
		else:
			$Player_Animated_Sprite.flip_h = true
	else:
		$Player_Animated_Sprite.play("idle")
	
	move_and_slide()

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