Skip to content

Instantly share code, notes, and snippets.

@identity2
Created June 8, 2019 13:01
Show Gist options
  • Save identity2/bbcaf01faf480b6d7c6001ad3bbd8901 to your computer and use it in GitHub Desktop.
Save identity2/bbcaf01faf480b6d7c6001ad3bbd8901 to your computer and use it in GitHub Desktop.
# This function should be called whenever the character moves.
func update_camera(character_pos):
var new_camera_pos = self.get_global_pos()
# Check if the character reaches the right margin.
if character_pos.x > self.get_global_pos().x + screen_size.width * (drag_margin_right - 0.5):
new_camera_pos.x = character_pos.x - screen_size.width * (drag_margin_right - 0.5)
# Check if the character reaches the left margin.
elif character_pos.x < self.get_global_pos().x + screen_size.width * (drag_margin_left - 0.5):
# Character reaches the left drag margin.
new_camera_pos.x = character_pos.x + screen_size.width * (0.5 - drag_margin_left)
# Clamp the new camera position within the limits.
new_camera_pos.x = clamp(new_camera_pos.x, left_limit + screen_size.width * 0.5, right_limit - screen_size.width * 0.5)
new_camera_pos.y = clamp(new_camera_pos.y, top_limit + screen_size.height * 0.5, bottom_limit - screen_size.height * 0.5)
# Actually update the position of the camera.
self.set_global_pos(new_camera_pos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment