Advertisement
Guest User

camera

a guest
Mar 7th, 2019
3,506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. extends Spatial
  2.  
  3. const MOVE_MARGIN = 20
  4. const MOVE_SPEED = 30
  5.  
  6. const ray_length = 1000
  7. onready var cam = $Camera
  8.  
  9. func _process(delta):
  10. var m_pos = get_viewport().get_mouse_position()
  11. calc_move(m_pos, delta)
  12. if Input.is_action_just_pressed("main_command"):
  13. move_all_units(m_pos)
  14.  
  15. func calc_move(m_pos, delta):
  16. var v_size = get_viewport().size
  17. var move_vec = Vector3()
  18.  
  19. if m_pos.x < MOVE_MARGIN:
  20. move_vec.x -= 1
  21. if m_pos.y < MOVE_MARGIN:
  22. move_vec.z -= 1
  23. if m_pos.x > v_size.x - MOVE_MARGIN:
  24. move_vec.x += 1
  25. if m_pos.y > v_size.y - MOVE_MARGIN:
  26. move_vec.z += 1
  27. move_vec = move_vec.rotated(Vector3(0, 1, 0), rotation_degrees.y)
  28. global_translate(move_vec * delta * MOVE_SPEED)
  29.  
  30. func move_all_units(m_pos):
  31. var result = raycast_from_mouse(m_pos, 1)
  32. if result:
  33. get_tree().call_group("units", "move_to", result.position)
  34.  
  35. func raycast_from_mouse(m_pos, collision_mask):
  36. var ray_start = cam.project_ray_origin(m_pos)
  37. var ray_end = ray_start + cam.project_ray_normal(m_pos) * ray_length
  38. var space_state = get_world().direct_space_state
  39. return space_state.intersect_ray(ray_start, ray_end, [], collision_mask)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement