Topdown Enemy that moves around without moving at diagonals

Godot Version

Godot 4.2


Hello, does know how to program a enemy who moves around without moving at diagonals in topdown game (every 3 or 5 seconds, he changes the directions any 4 direction) and if a enemy collided with wall or identity he goes to almost any other side like if enemy is moving right and then touches the wall/identity, he moves up, down or left, can anyone help me with script or any tip?

set a timer or just put a timer variable and accumulate time delta to trigger a function of movement

for the movement, make it like

The MOVEMENT_SPEED constant basically just how many pixel it will move to every time the random_move() is called

const MOVEMENT_SPEED:float=10

func random_move():

above codes just for moving Sprite2D to random position, this doesnt detect if it hit a wall or not, but if it’s a CharacterBody2D, then just randomize the velocity instead. Because CharacterBody will take account of collisionshape, it should just stop at hitting wall when random_move() occurs, which you could actually use the collision data to determine if it will go the opposite direction instead after hitting the wall