+1 vote

Hi,

I need to create the enemy that moves around the wall 1x1 (so it would make him walk on square 3x3).
So I want it to walk left-down-right-up etc., but only straight lines, not in a circle, but I don't know how should the script look like.
I made already enemies that walk only up-down and left-right, for example the script looks like this:

extends Area2D

var enemyMovement = 40
var enemyMovementLong = 32 * 9

onready var enemyStartPositionY = self.position.y

func _physics_process(delta):
    move_local_y(enemyMovement * delta)
    if (self.position.y < enemyStartPositionY):
        enemyMovement = - enemyMovement
    elif (self.position.y > enemyStartPositionY + enemyMovementLong):
        enemyMovement = - enemyMovement

func _on_enemyUpDown_area_entered(area):
    get_tree().reload_current_scene()

Can you please help me?

Godot version Godot v3.5.1
in Engine by (13 points)

1 Answer

0 votes

Honestly this sounds like a job for AStar2D
A simpler solution would be an Array of directions

var enemyMovement = 40
var enemyMovementLong = 32 * 9

onready var enemyStartPositionY = self.position.y
var commands = ["right", "down", "left", "up"]

func _physics_process(delta):
    if commands.empty():
        return
    match commands.pop_front():
        "right":
            #move right
        "down":
            #move down
        "left":
            #move left
        "up"
            #move up

This way you can populate multi-direction commands and even change how your enemy behaves on the fly

by (6,876 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.