Mob stopped walking after restarting the godot editor

Mobs stopped walking after restarting the godot editor. I made the game according to the video tutorials. Everything was working fine before quitting the program. Today I continued making the game, but the mobs are no longer walking. The walking animation starts when I approach the mob, but the mob stands still. help please. The code is attached.

The moba code is below. I apologize for my inexperience, I’m just learning.

extends CharacterBody2D

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

var chase = false
var speed = 100
var alive = true
@onready var anim = $AnimatedSprite2D

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
var player = $“…/…/Player/Player”
var diretion = (player.position - self.position).normalized()
if alive == true:
if chase == true:
velocity.x = diretion.x * speed
anim.play(“run”)
else:
velocity.x = 0
anim.play(“idle”)
if diretion.x < 0:
$AnimatedSprite2D.flip_h = true
else:
$AnimatedSprite2D.flip_h = false

func _on_area_2d_body_entered(body):
if body.name == “Player”:
chase = true

func _on_area_2d_body_exited(body):
if body.name == “Player”:
chase = false

func _on_area_2d_2_body_entered(body):
if body.name == “Player”:
body.velocity.y -= 300
death()

func _on_death_2_body_entered(body):
if body.name == “Player”:
if alive == true:
body.health -=40
death()

func death():
alive = false
anim.play(“death”)
await anim.animation_finished
queue_free()

It’s always a good idea to give a source. There’s nothing like “the video tutorials”. If you want others to help you, be as precise as you can.

Also, please format your code properly by clicking on the “</>” button when making a post. That way the indentation gets preserved, and it’s a lot easier to read.

Regarding your issue: You’re using a CharacterBody2D, but seem to never call move_and_collide or move_and_slide to actually apply the velocity!

func _physics_process(delta):
	# ...
	if alive == true:
		if chase == true:
			velocity.x = diretion.x * speed
			move_and_slide() # <-- new!
			anim.play(“run”)
	# ...

Thank you. You helped me.

1 Like