I would like to know what the best way to use a following camera is when using a subscene for a player.
Within my game the player can transform between two different forms, so the subscene starts with a node2D, and thus cannot be directly followed by setting a camera as a child.
My game is going to be a metroidvania, so there will be a large amount of scenes.
I also want to dynamically change the bounds of the camera in a room when the player reaches certain coordinates or conditions for more game feel later.
I currently have 2 different options of following the player, and would like to know which is best, or if there is a better one.
Option 1: 2 Camera’s in the subscene
In this option I have a camera follow each of the transformations a player can be in.
Whenever the player transforms, the active camera swaps, so that the current form stays within view.
Scene tree:
Code (running on the Player Node2D):
func _switch_form():
# ----- other code handling transformations
# Set the camera to focus on the new form
current_form.get_node("camera").enabled = false
other_form.get_node("camera").enabled = true
This option follows the player smoothly, however it does make setting the bounds for the camera more difficult, as I need to send them to the player node each time the scene switches, and there are two of them.
Option 2: 1 camera in the level scene
In this option I have a camera in every level scene setup for the scene itself.
In here it follows the active Charcterbody2D node the player is controlling.
Scene tree:
Code (running on the Camera2D):
func _ready():
# Setup variables
player_node = $"../Player"
camera_node = $"../Camera"
form1_player_node = $"../Player/Form1"
form2_player_node = $"../Player/Form2"
active_node = "form1"
# Snap Camera to the player
camera_node.position = player_node.position
func _process(delta):
# Check which charcterbody needs to be followed
if(active_node == "form1"):
# Set the camera to the correct position
camera_node.position = player_node.position + form1_player_node.position
else:
# Set the camera to the correct position
camera_node.position = player_node.position + form2_player_node.position
# If the player transforms, swap the Charcterbody the camera will follow
if Input.is_action_just_pressed("transform"):
if (active_node == "form1"):
active_node = "form2"
else:
active_node = "form1"
This option follows the player less smooth, stopping and starting snaps the camera for example. However, it allows far more easy control of the bounds for each scene.
Which of these two options is better, and should I use?
Are there also any improvements I can make to the code, or any alternative methods I can use to get the camera to follow the player?