Advertisement
chris33556

chatGPT enemy AI pathfinder script

Dec 18th, 2022
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. # Speed of the character in units per second
  4. export var speed = 3
  5.  
  6. # List of points representing the path to follow
  7. var path = []
  8.  
  9. # Current index in the path list
  10. var cur_path_idx = 0
  11.  
  12. # Target position to move towards
  13. var target = null
  14.  
  15. # Current velocity of the character
  16. var velocity = Vector3.ZERO
  17.  
  18. # Distance threshold for reaching a target position
  19. var threshold = .1
  20.  
  21. # Reference to the Navigation node
  22. onready var nav = get_parent()
  23.  
  24. # Reference to the AnimationPlayer node
  25. onready var animatedSprite = get_node("AnimatedSprite3D")
  26.  
  27. func _ready():
  28. # Wait for the owner and player nodes to be ready
  29. yield(owner, "ready")
  30. yield(get_node("../../player"), "ready")
  31.  
  32. # Set the target position to the player's global transform origin
  33. target = get_node("../../player").global_transform.origin
  34.  
  35. func _physics_process(delta):
  36. # Update the character's movement and animation
  37. update_movement()
  38. update_animation()
  39.  
  40. func update_movement():
  41. if path.size() > 0:
  42. # Follow the path if there is one
  43. move_along_path()
  44. else:
  45. # Move towards the target position if there is no path
  46. move_towards_target()
  47.  
  48. func move_along_path():
  49. if cur_path_idx >= path.size():
  50. return
  51.  
  52. if global_transform.origin.distance_to(path[cur_path_idx]) < threshold:
  53. # Advance to the next point in the path if the character is close enough
  54. cur_path_idx += 1
  55.  
  56. else:
  57. # Calculate the direction to the next point in the path
  58. var direction = path[cur_path_idx] - global_transform.origin
  59. velocity = direction.normalized() * speed
  60. move_and_slide(velocity, Vector3.UP)
  61.  
  62. func move_towards_target():
  63. if cur_path_idx >= path.size():
  64. return
  65.  
  66. if global_transform.origin.distance_to(path[cur_path_idx]) < threshold:
  67. cur_path_idx += 1
  68.  
  69. else:
  70. var direction = path[cur_path_idx] - global_transform.origin
  71. velocity = direction.normalized() * speed
  72. move_and_slide(velocity, Vector3.UP)
  73.  
  74. func update_animation():
  75. if velocity.length() > 0:
  76. # Play the "Walk" animation if the character is moving
  77. $AnimatedSprite3D.play("walking")
  78. else:
  79. # Stop the current animation if the character is not moving
  80. $AnimatedSprite3D.play("idle")
  81.  
  82. func get_target_path(target_pos):
  83. # Calculate the simple path to the target position
  84. path = nav.get_simple_path(global_transform.origin, target_pos)
  85. cur_path_idx = 0
  86.  
  87. func _on_Timer_timeout():
  88. # Update the target position and path every frame
  89. target = get_node("../../player").global_transform.origin
  90. get_target_path(target.global_transform.origin)
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement