I think that only works if the Camera2D is a child of the node perhaps? I've got the Camera2D as a child of the main scene and want it to focus on specific nodes (which are their own scenes). I've tried putting separate cameras on each node and switching them to active which works but then my camera scrolling behaviour gets messed up (much faster and the x and y positions seem to become inverted).
Here is my camera script:
extends Camera2D
onready var system = get_tree().get_current_scene()
var mouse = get_global_mouse_position()
export (int) var camera_margin = 2
func _ready():
system.connect("focus_camera", self, "_on_focus_camera")
func _on_focus_camera():
var camera = globalvar.selected_unit.get_node("Camera/Camera2D")
camera.current = true
func _process(delta):
var pos = get_viewport().get_mouse_position()
var rec = get_viewport().get_visible_rect()
if rec.size.x - pos.x <= camera_margin:
self.position = get_global_mouse_position()
if pos.x <= camera_margin:
self.position = get_global_mouse_position()
if rec.size.y - pos.y <= camera_margin:
self.position = get_global_mouse_position()
if pos.y <= camera_margin:
self.position = get_global_mouse_position()