camera_orbit.gd 829 B

1234567891011121314151617181920212223242526272829303132333435
  1. extends Camera3D
  2. const ROTATION_COEFF = 0.02
  3. var _rotation_enabled = false
  4. var _rotation_pivot
  5. func _ready():
  6. call_deferred("_initialize_pivot")
  7. func _unhandled_input(event):
  8. var mouse_button_event = event as InputEventMouseButton
  9. if mouse_button_event:
  10. if mouse_button_event.button_index == MOUSE_BUTTON_RIGHT:
  11. _rotation_enabled = mouse_button_event.pressed
  12. return
  13. if not _rotation_enabled:
  14. return
  15. var mouse_motion_event = event as InputEventMouseMotion
  16. if mouse_motion_event:
  17. var rotation_delta = mouse_motion_event.relative.x
  18. _rotation_pivot.rotate(Vector3.UP, -rotation_delta * ROTATION_COEFF)
  19. func _initialize_pivot():
  20. _rotation_pivot = Node3D.new()
  21. var camera_parent = get_parent()
  22. camera_parent.add_child(_rotation_pivot)
  23. camera_parent.remove_child(self)
  24. _rotation_pivot.add_child(self)