I am new to Godot, transitioning from Unity and I still do not have a good grasp on how scenes and viewports interact. I am making a game with two players: one pilots an aircraft and the other shoots a turret on top of it. Each player has a different camera, so I want the game to be split-screen. Most tutorials for Godot 3D implement split-screens like this:
Spatial
Viewport1
Player1
Camera1
Viewport2
Player2
Camera2
However, that solution does not work for me because it separates the cameras, and I would like to have both cameras together in a single scene like this:
Player
Body
Camera1
Turret
Camera2
I tried this:
Control
GridContainer
Player
With the player already having 2 cameras as described previously, but only 1 camera gets displayed. Is there any good way to implement this? Alternatively, I could make the body and the turret separate scenes and make them follow each other with code, but getting them together in a single scene simplifies other problems.
Sadly the setters for the
Camera
(3D
) inViewport
are not exposed to scripting (this is because we are supposed to use thecurrent
property on theCamera
(3D
)).But we can do the change at a lower level by calling
viewport_attach_camera
on theRenderingServer
(in Godot 4) orVisualServer
(in Godot 3) to change the currentCamera
(3D
) of theViewport
to an arbitrary one with .The method wants the
RID
(Resource ID) of theViewport
(which you get by callingget_viewport_rid
on it) and of theCamera
(3D
) (which you can get by callingget_camera_rid()
on it).This also means that the code that does this setup must be able to reference both the
Viewport
and its intendedCamera
(3D
).