Advertisement
RecluseStudio

Player Script for top down movement

Nov 21st, 2020 (edited)
1,315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3. var speed = 160
  4. var bullet = preload("res://Bullet.tscn")
  5. var can_fire = true
  6. var shooting = false
  7. var rate_of_fire = 0.08
  8. var move_target = Vector2(-670,-647)
  9. var shoot_target = Vector2()
  10. var velocity = Vector2()
  11.  
  12. func _input(event):
  13.     if Input.is_action_pressed("move_to"):
  14.         move_target = get_global_mouse_position()
  15.         look_at(move_target)
  16.     if Input.is_action_pressed("fire"):
  17.         shoot_target = get_global_mouse_position()
  18.  
  19. func _physics_process(delta):
  20.     MoveLoop()
  21.     ShootLoop()
  22.  
  23. func MoveLoop():
  24.     velocity = position.direction_to(move_target) * speed
  25.     if position.distance_to(move_target) > 1:
  26.         velocity = move_and_slide(velocity)
  27.  
  28. func ShootLoop():
  29.     if Input.is_action_pressed("fire") and can_fire == true:
  30.         can_fire = false
  31.         shooting = true
  32.         speed = 75
  33.         look_at(shoot_target)
  34.         get_node("playerTurnAxis").rotation = get_angle_to(get_global_mouse_position())
  35.         var bullet_instance = bullet.instance()
  36.         bullet_instance.position = get_node("playerTurnAxis/bulletSpawnPoint").get_global_position()
  37.         bullet_instance.look_at(get_global_mouse_position())
  38.         get_parent().add_child(bullet_instance)
  39.         yield(get_tree().create_timer(rate_of_fire), "timeout")
  40.         can_fire = true
  41.         shooting = false
  42.         speed = 160
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement