how should we make AI to be active properly?

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

Hi everyone.
i created an AI (kinematicbody 2d) for a fighting game. it moves and follows like what i designed, but the problem is, it fights only when i move my character, even when i move mouse cursor on screen without clicking, somehow it make ai to be active and fights!!!
but when i don’t do anything, it seems detection goes to inactive mode. i used overlapping areas method and everything works but only if i move other player or hit something on keyboard or move mouse cursor… how can i make ai to be active independently?

beside i use lerp for the ai because it moved with vibration, but now it moves like airplane :smiley: …what method should i use for ai to move like normal dude?

could someone help me with these problems? thanks
(godot v2.1.5)

We need to see AI script.

Oen44 | 2018-09-20 21:56

hi oen44. this is the ai code… just remember, everything is ok, but when ai come closer to other player, it does not punch, i have to move other player to activate ai or hit a random keyboard’s key or just moving mouse cursor in game scene, i dont know why. this is my main problem. i think i should play with lerp variables to solve airplane like movement and thats not really matter, thank you for your time :slight_smile:

extends KinematicBody2D
 
 export var speed = 10
 var velocity = Vector2()
 var smooth_velocity = Vector2()
 var counter = 0
 var block = 0
 var fist_pressed = true     #boolean for timer for controll animations to play completely
 var upper_pressed = true     #boolean for timer for controll animations to play completely
 var left_fu = false      #boolean for defence act
 var right_fu = false      #boolean for defence act
 var xx = null   #these four variables are for changing randomly speed of the ai that i think they dont work properly
 var yy = null  #but thats not matter for now i think
 var xr = null  #
 var yl = null  #
 var rand_defence = null  #create random int to active defence mode sometimes
 var right_act = null #variables for moving areas to detect Whether enemy's head is infront of ai or not 
 var left_act = null  #
 
 #NoDefence Timer Variables
 var no_defence_delay = .4
 var no_defence_timer = null
 var no_defence_bool = true
 
 onready var enemy = get_tree().get_root().get_node("/root/PvAI/AI-P01")
 onready var main_scene = get_tree().get_root().get_node("/root/PvAI")
 
 onready var sprite = get_node("Sprite")
 onready var animations = get_node("Sprites") #animation player for animations
 onready var shapes = get_node("Shapes") #animation player for fighting areas
 onready var shape_signals = get_node("ShapeSignals") #animation player for detecting enemy'head
 onready var idle = get_node("Idle") #the idle shape for ai
 onready var head_f = get_node("HeadF")  #head_f and head_u are for detecting various punch
 onready var head_u = get_node("HeadU")  #
 onready var defence = get_node("Defence") #defence shape
 onready var fist_timer= get_node("FistTimer")
 onready var upper_timer = get_node("UpperTimer")
 onready var right_fists_area = get_node("RUpperFistArea") #main areas for shape_signals animation player
 onready var left_fists_area = get_node("LUpperFistArea") #main areas for shape_signals animation player
 onready var enemy_area = get_tree().get_root().get_node("/root/PvAI/AI-P01/PlayerPosition") #use this one for ai to detect                                                                                                                                                       # player and stand infront of it...
 onready var ray_m = get_node("RayTurnM") #some raycast for detecting and do various #animation and for flipping
 onready var ray_r = get_node("RayTurnR")
 onready var ray_l = get_node("RayTurnL")
 onready var ray_rm = get_node("RayTurnRM")
 onready var ray_lm = get_node("RayTurnLM")
 
 func _ready():
      set_fixed_process(true)
      set_process_input(true)
      #create timer for NoDefence
      no_defence_timer = Timer.new()
      no_defence_timer.set_one_shot(true)
      no_defence_timer.set_wait_time(no_defence_delay)
      no_defence_timer.connect("timeout", self, "no_defence_timeout")
      add_child(no_defence_timer)
 #timeout function for NoDefence
 func no_defence_timeout():
      no_defence_bool = true
 
 func _fixed_process(delta):
      if shape_signals.is_playing():
         pass
      else:
         shape_signals.queue("Left")
         shape_signals.queue("Right")
      velocity = Vector2()
      xx = randi()
      yy = randi()
      if enemy_area.get_global_pos().x>self.get_global_pos().x:
         velocity.x += xx
      if enemy_area.get_global_pos().x<self.get_global_pos().x:
         velocity.x -= xx
      if enemy_area.get_global_pos().y>self.get_global_pos().y:
         velocity.y += yy
      if enemy_area.get_global_pos().y<self.get_global_pos().y:
         velocity.y -= yy
 
      velocity = velocity.normalized()*speed
      smooth_velocity.x = lerp(smooth_velocity.x, velocity.x, delta)
      smooth_velocity.y = lerp(smooth_velocity.y, velocity.y, delta)
      var motion = move(smooth_velocity)
      if (is_colliding()):
         var n = get_collision_normal()
         motion = n.slide(motion)
         smooth_velocity = n.slide(smooth_velocity)
         move(motion)
      
      if ray_r.is_colliding() || ray_l.is_colliding() || ray_m.is_colliding() || ray_rm.is_colliding() || ray_lm.is_colliding():
         pass
      else:
         if enemy.get_global_pos().x>(self.get_global_pos().x+OS.get_window_size().x*0.12):
            set_rotd(-90)
         if enemy.get_global_pos().x<(self.get_global_pos().x-OS.get_window_size().x*0.12):
            set_rotd(90)

mdswamp | 2018-09-20 23:34

   func _input(event):
             
          defence.set_trigger(true)
          defence.hide()
          block = 0
          xr = randi()%2
          yl =  randi()%2
          rand_defence = randi()%3
          right_act = right_fists_area.get_overlapping_areas()
          if (right_act.size() != 0):
             for heads in right_act:
                 if heads.is_in_group("head"):
                    if fist_pressed == true && upper_pressed == true:
                       fist_pressed = false
                       upper_pressed = false
                       if enemy.block == 0:
                          if (xr == 0):
                             animations.play("R_Fist")
                             shapes.play("FR")
                          if (xr == 1):
                             animations.play("R_Upper")
                             shapes.play("UR")
                          fist_timer.start()
                         upper_timer.start()
                       else:
                          if ray_rm.is_colliding() || ray_lm.is_colliding() || ray_m.is_colliding():
                             if (xr == 0):
                                animations.play("Half_R")
                             if (xr == 1):
                                animations.play("R_Upper")
                                shapes.play("UR")
                          else:
                             if (xr == 0):
                                animations.play("R_Fist")
                                shapes.play("FR")
                             if (xr == 1):
                                animations.play("R_Upper")
                                shapes.play("UR")
                          fist_timer.start()
                          upper_timer.start()
          left_act = left_fists_area.get_overlapping_areas()
          if (left_act.size() != 0):
             for heads in left_act:
                 if heads.is_in_group("head"):
                    if fist_pressed == true && upper_pressed == true:
                       fist_pressed = false
                       upper_pressed = false
                       if enemy.block == 0:
                          if (yl == 0):
                             animations.play("L_Fist")
                             shapes.play("FL")
                          if (yl == 1):
                             animations.play("L_Upper")
                             shapes.play("UL")
                          fist_timer.start()
                          upper_timer.start()
                       else:
                          if ray_rm.is_colliding() || ray_lm.is_colliding() || ray_m.is_colliding():
                             if (yl == 0):
                                animations.play("Half_L")
                             if (yl == 1):
                                animations.play("L_Upper")
                                shapes.play("UL")
                          else:
                             if (yl == 0):
                                animations.play("L_Fist")
                                shapes.play("FL")
                             if (yl == 1):
                                animations.play("L_Upper")
                                shapes.play("UL")
                          fist_timer.start()
                          upper_timer.start()
          if (right_fu == true || left_fu == true) && (rand_defence == 0 || rand_defence == 2):
             right_fu = false
             left_fu = false
             if (no_defence_bool == false):
                animations.play("Idle")
                block = 0
                defence.set_trigger(true)
                defence.hide()
                no_defence_timer.start()
             else:
                animations.play("Defence")
          if animations.get_current_animation() == "Defence":
             defence.set_trigger(false)
             defence.show()
             block = 1
          if event.is_action_released("p02_defence"):
             animations.play("Idle")
             block = 0
             defence.set_trigger(true)
             defence.hide()
     
     func _on_RightFist_area_enter( area ): #Player01
          if (head_f.get_collision_mask() == 32 && block == 0):
             animations.queue("Hurt")
             counter+=1 #Player01 Points
     func _on_LeftFist_area_enter( area ): #Player01
          if (head_f.get_collision_mask() == 32 && block == 0):
             animations.queue("Hurt")
             counter+=1 #Player01 Points
     func _on_RightUpper_area_enter( area ): #Player01
          if (head_u.get_collision_mask() == 128 && block == 0):
             animations.queue("Hurt")
             counter = counter+2 #Player01 Points
          if (head_u.get_collision_mask() == 128 && block == 1 && no_defence_bool):
             animations.play("NoDefence")
             block = 0
             defence.set_trigger(true)
             defence.hide()
             no_defence_bool = false
     func _on_LeftUpper_area_enter( area ): #Player01
          if (head_u.get_collision_mask() == 128 && block == 0):
             animations.queue("Hurt")
             counter = counter+2 #Player01 Points
          if (head_u.get_collision_mask() == 128 && block == 1 && no_defence_bool):
             animations.play("NoDefence")
             block = 0
             defence.set_trigger(true)
             defence.hide()
             no_defence_bool = false
     
     func _on_FistTimer_timeout():
          fist_pressed = true
          upper_pressed = true
     
     func _on_UpperTimer_timeout():
          upper_pressed = true
          fist_pressed = true
     
     
     func _on_RUpperFistArea_area_enter( area ):
          if right_fists_area.get_collision_mask() == 256:
             right_fu = true
     
     
     func _on_LUpperFistArea_area_enter( area ):
          if left_fists_area.get_collision_mask() == 256:
             left_fu = true

mdswamp | 2018-09-20 23:34

Please use Github or at least Pastebin to share code that long.

Oen44 | 2018-09-21 09:18

:bust_in_silhouette: Reply From: Oen44

You are using _input(event) on AI, why? That event is being fired ONLY on key press or mouse movement. All that stuff should be in _fixed_process. If you want your AI to block incoming attack then don’t tell AI that player pressed punch key. You have to use stuff like player.state == STATE_PUNCH, player.state == STATE_RUN etc.

:expressionless: :expressionless: :expressionless:
hi oen44… id copied my player2 codes in ai.gd … about block incoming attack, i hadn’t seen that one :smiley: thank you very much, i dont know why i didnt remove the input function and put those codes in fixed process :expressionless: :expressionless:

mdswamp | 2018-09-21 12:19