Advertisement
chris33556

enemy pathfinding

Dec 18th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. export var speed = 3
  4.  
  5. var path = []
  6. var cur_path_idx = 0
  7. var target = null
  8. var velocity = Vector3.ZERO
  9. var threshold = .1
  10.  
  11. onready var nav = get_parent()
  12.  
  13. func _ready():
  14. yield(owner, "ready")
  15. target = owner.player
  16.  
  17. func _physics_process(delta):
  18. if path.size() > 0:
  19. move_to_target()
  20.  
  21. func move_to_target():
  22. if cur_path_idx >= path.size():
  23. return
  24.  
  25. if global_transform.origin.distance_to(path[cur_path_idx]) < threshold:
  26. cur_path_idx += 1
  27.  
  28. else:
  29. var direction = path[cur_path_idx] - global_transform.origin
  30. velocity = direction.normalized() * speed
  31. move_and_slide(velocity, Vector3.UP)
  32.  
  33. func get_target_path(target_pos):
  34. path = nav.get_simple_path(global_transform.origin, target_pos)
  35. cur_path_idx = 0
  36.  
  37. func _on_Timer_timeout():
  38. get_target_path(target.global_transform.origin)
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement