Advertisement
Zrko

enemy ai tutorial

May 6th, 2023 (edited)
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 2.35 KB | Source Code | 0 0
  1. extends CharacterBody3D
  2.  
  3. enum {
  4.     SLEEP,
  5.     AWAKE,
  6.     CHASE,
  7.     ATTACK,
  8.     WAIT
  9. }
  10.  
  11. var CURRENT_STATE = SLEEP
  12.  
  13. var cooldown = []
  14. var stats = {
  15.     "max_health": 100,
  16.     "health": 100,
  17.     "default_speed": 4.0,
  18.     "speed": 0.0,
  19.     "attack_speed": 20.0
  20. }
  21.  
  22.  
  23. @onready var agent = $NavigationAgent3D
  24. @onready var plr = get_node(PLAYER_PATH)
  25. @onready var anim = $Anim
  26.  
  27. func _ready():
  28.     stats["speed"] = stats["default_speed"]
  29.     $Indicator.visible = false
  30.  
  31. func _physics_process(delta):
  32.     if CURRENT_STATE == CHASE or CURRENT_STATE == ATTACK:
  33.         var next_location = agent.get_next_path_position()
  34.         var final_velocity = (next_location - global_transform.origin).normalized() * stats["speed"]
  35.        
  36.         agent.set_velocity(final_velocity)
  37.    
  38.     if CURRENT_STATE == SLEEP: $Sight/Raycasts.look_at(plr.position, Vector3.UP);
  39.     state_handler()
  40.    
  41. func awaken():
  42.     if cooldown.has("awake") == true: return
  43.     cooldown.append("awake")
  44.    
  45.     $Indicator.visible = true
  46.     anim.play("Shock")
  47.    
  48.     await get_tree().create_timer(0.6).timeout
  49.    
  50.     $Indicator.visible = false
  51.     CURRENT_STATE = CHASE
  52.    
  53. func attack():
  54.     cooldown.append("attack_cd")
  55.     anim.play("Attack")
  56.     stats["speed"] = stats["attack_speed"]
  57.    
  58.     await get_tree().create_timer(0.4).timeout
  59.    
  60.     CURRENT_STATE = WAIT
  61.     stats["speed"] = stats["default_speed"]
  62.    
  63.     await get_tree().create_timer(0.4).timeout
  64.    
  65.     cooldown.erase("attack_cd")
  66.     CURRENT_STATE = CHASE
  67.    
  68. func state_handler():
  69.     match CURRENT_STATE:
  70.         SLEEP:
  71.             stats["speed"] = 0
  72.             anim.play("Sleep")
  73.            
  74.             if check_in_range() == true:
  75.                 CURRENT_STATE = AWAKE
  76.            
  77.         AWAKE:
  78.             awaken()
  79.        
  80.         CHASE:
  81.             stats["speed"] = stats["default_speed"]
  82.             anim.play("Run")
  83.             agent.set_target_position(plr.position)
  84.        
  85.         WAIT:
  86.             anim.play("Shock")
  87.             stats["speed"] = 0
  88.        
  89.         ATTACK:
  90.             if not cooldown.has("attack_cd"):
  91.                 attack()
  92.    
  93. func check_in_range():
  94.     for ray in $Sight/Raycasts.get_children():
  95.         if ray.is_colliding():
  96.             var obj = ray.get_collider()
  97.            
  98.             if obj.name == "PLAYER NAME OR GROUP HERE":
  99.                 return true
  100.             else:
  101.                 return false
  102.  
  103. # In range
  104. func _on_navigation_agent_3d_target_reached():
  105.     CURRENT_STATE = ATTACK
  106.  
  107. func set_float():
  108.     if CURRENT_STATE == ATTACK: return 1
  109.     else: return 0.5
  110.  
  111. func _on_navigation_agent_3d_velocity_computed(safe_velocity):
  112.     velocity = velocity.move_toward(safe_velocity, set_float())
  113.     move_and_slide()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement