My automatic enemy fails and sometimes invades my blocks like a wall

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

Well, I’m creating a game with blocks, like a maze. Where I have my character who must go through the enemies and reach the end of the stage.
But my enemy has a bug and sometimes invades the blocks and goes crazy. I’ll post a video reporting the error and the code made.
I’m Brazilian so maybe my English isn’t perfect.
This is my code:

extends StaticBody2D

const DOWN = 0
const RIGHT = 1
const UP = 2
const LEFT = 3

export var speed = 200
export var health = 1
var velocity = Vector2()
var move_direction = -1
var direction
var steps
var count_steps

func _ready():
randomize()
_new_goal()

func _new_goal():
direction = randi() % 4
steps = 50 + (randi() % 50)
count_steps = 0

func _process(delta):
count_steps += 1
if count_steps > steps:
_new_goal()
if direction == DOWN:
velocity.y = speed
rotation_degrees = 0
elif direction == RIGHT:
velocity.x = speed
rotation_degrees = -90
elif direction == UP:
velocity.y = -speed
rotation_degrees = 180
elif direction == LEFT:
velocity.x = -speed
rotation_degrees = 90
if velocity.length() > 0:
pass

:bust_in_silhouette: Reply From: Cire_arievilo1

Se postar o vídeo ou um gif ajudaria a ter mais informações do problema.

Talvez seja o “extends StaticBody2D” se fosse um area2D ou um kinematicBody2D pode ser que resolva o problema.