Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. # This function should be called whenever the character moves.
  2. func update_camera(character_pos):
  3. var new_camera_pos = self.get_global_pos()
  4.  
  5. # Check if the character reaches the right margin.
  6. if character_pos.x > self.get_global_pos().x + screen_size.width * (drag_margin_right - 0.5):
  7. new_camera_pos.x = character_pos.x - screen_size.width * (drag_margin_right - 0.5)
  8.  
  9. # Check if the character reaches the left margin.
  10. elif character_pos.x < self.get_global_pos().x + screen_size.width * (drag_margin_left - 0.5):
  11. # Character reaches the left drag margin.
  12. new_camera_pos.x = character_pos.x + screen_size.width * (0.5 - drag_margin_left)
  13.  
  14. # Clamp the new camera position within the limits.
  15. new_camera_pos.x = clamp(new_camera_pos.x, left_limit + screen_size.width * 0.5, right_limit - screen_size.width * 0.5)
  16. new_camera_pos.y = clamp(new_camera_pos.y, top_limit + screen_size.height * 0.5, bottom_limit - screen_size.height * 0.5)
  17.  
  18. # Actually update the position of the camera.
  19. self.set_global_pos(new_camera_pos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement