Adding a "Scope" to a Twinstick/Topdown-Shooter

Godot Version

Godot v4.2.2 (windows64)

Question

Hi, i am currently making a Twinstick/TopDown-Shooter and i wanna create a “Scoping” mechanism for some Weapons.
I wanna add a Window inside the Main/Character-Camera, that shows the View of a Second Camera, which can go outside the first Cameras-Border.
What i coded: (I added a second Camera to my Gun node)
func _physics_process(delta):
if Input.is_action_pressed(“Aim”):

cameraUpdate()
func cameraUpdate():
var pos = get_local_mouse_position()

if pos.x >= -250 and pos.x < 250:

set_position(pos)
obviosly this doesn’t work, since it is a Camera to my GUN, and does not affect my Main/Character-Camera
This code does work tho, when used in my MainCamera as the:
“I want the camera to go so far, unti the border touches my Character/CharacterHitbox”
tho, it does not Reset the camera, after releasing “aim” (yet)
obviosly i need to add a
[if Input.is_action_just_released(“Aim”):
Blabla Camera Position = reset]
and it will be fixed.
But, i realy don’t know how to ad a Window, inside my MainCamera that shows the View of the Second/Scope-Camera.
Any idea?

I think your tree should look something like that. I used a subViewport to render the view of the zoomed in camera to a vireportTexture I overlayed as an ui element. The marginContainer just makes sure there is a gap between the viewport texture and the border of the display. (You can change that in the theme overrides of the marginContainer, set Layout > Anchors Preset to full rect)

Bild_2024-09-12_212331229

There is a common bug with the CanvasLayer:

  • if you check the ‘follow camera’ checkbox it usually does not work until you add another CanvasLayer node and use the new one (then you can delete the old one)

I didn’t get the subviewport to render out the whole scene though. It just renders its children appearently (not qiet sure if thats a bug or if I didn’t set it up correctly)

But I think that would be the way to go.
I hope that helps in any way.

1 Like

At Least i now know, how to get a Window of the second Camera into the Main-Camera-scene. I’ll figure out how to move the second Camera eventually…

Thanks, this already helped!

EDIT : I can’t figure out, why the Subviewport is not showing, what the “Outside the Main Camera”-Camera see’s.