camera_tracker.gd 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. class_name CameraTracker
  2. extends Camera3D
  3. ## Track object in subject view
  4. @export var max_distance: float = 10
  5. @export var camera_height: float = 1
  6. var _tracked_node: Node3D
  7. func track(node: Node3D) -> void:
  8. _tracked_node = node
  9. func _process(_delta: float) -> void:
  10. if _tracked_node == null:
  11. return
  12. look_at(_tracked_node.get_global_position())
  13. _adjust_distance()
  14. func _adjust_distance() -> void:
  15. var target: Vector3 = _tracked_node.get_global_position()
  16. _adjust_max_distance_from(target)
  17. _adjust_min_distance_from(target)
  18. func _adjust_max_distance_from(target: Vector3) -> void:
  19. var current_distance: float = position.distance_to(target)
  20. if current_distance > max_distance:
  21. var diff: float = current_distance - max_distance
  22. position += transform.basis * Vector3.FORWARD * diff
  23. func _adjust_min_distance_from(target: Vector3) -> void:
  24. var target_height: float = target.y + camera_height
  25. if position.y < target_height:
  26. position.y = target_height
  27. func _on_focus_required(node: Node3D) -> void:
  28. track(node)