0 votes

I am trying to make something like an AI for an enemy, when the game starts, the enemy walks in a random direction, and when it collides with an area, it changes direction or keeps going forward, but none of that works here is my project structure:


# Enemy script
extends ***************

  var direction = Vector2(0, 0)
  onready var anim = get_node("anim")
  const enemy_speed = 0.1
  const speed = 200

 func _ready():

 func _fixed_process():
         var num = randomize()
         if(num == 1):
        elif(num == 2):

     if (is_colliding()):
      print("Collision with ",get_collider())
      var n = get_collision_normal()
      direction = n.slide( direction )
      move(direction * speed * delta) 

 func move_up():
    direction += Vector2(0, -enemy_speed)
    if(not anim.is_playing() or anim.get_current_animation().basename() !='walk_back'):

here is the Node2D script

#Node script
 extends Node2D

 var num                  = range(1, 2)
 onready var enemy        = get_node("enemy")
 onready var area00       = get_node("areas_ai/area_ai")
 onready var area01        = get_node("areas_ai/area_ai01")

 func _ready():
    area00.connect("body_enter", self, "_area_zero")
        area01.connect("body_enter", self, "_area_one")

 func _area_zero(body):

 func _area_one(body):
       if(num == 1):
       if(num == 2):

I also want the random number to keep randomizing between 1 and 2 and even 1, 2, and 3, and when like maybe the number is 1 the enemy would move up, when the number is 2, the number would move down, or when the number is three, it would move left, or something like that, but i have no idea on how

in Engine by (119 points)
edited by

1 Answer

0 votes

While you do want to call randomize() once, you don't need to call it in fixed_process.

Also, randomize() doesn't return anything, so var num = randomize() doesn't work, you'll probably want var num = randi() % 2 instead (in your enemy script's fixed_process), then compare num to 0 and 1, not 1 and 2

by (346 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.