Advertisement
otorp2

simple chase player

Mar 20th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. extends Node2D
  2.  
  3. var enemySpeed = 7
  4.  
  5. onready var enemy_pos = get_node("enemy").get_pos()
  6. onready var player_pos = get_node("player").get_pos()
  7. onready var enemy_node = get_node("enemy")
  8.  
  9. func _ready():
  10.  
  11. set_process(true)
  12. pass
  13.  
  14. func _process(delta):
  15.  
  16. var dx = player_pos.x - enemy_pos.x
  17. var dy = player_pos.y - enemy_pos.y
  18. var distance = sqrt(dx * dx + dy * dy)
  19. enemy_pos.x = enemy_pos.x + dx / distance * enemySpeed
  20. enemy_pos.y= enemy_pos.y + dy / distance * enemySpeed
  21. enemy_node.set_pos(enemy_pos)
  22.  
  23. print(dx, "",dy)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement