• 2D
  • How do I Vertically align the viewport to Not the top when it becomes bigger than its limits?

I set the Stretch Aspect to "Expand" because I want my game to support multiple aspect ratios without Black Bars.

Now I have the problem that the viewport, when it exceeds the Camera2D's vertical limits, always extends towards the Bottom (Left Image) when I actually want it to extend towards the Top (Middle Image) or in both directions (Right Image).

How can I change the vertical viewport extension direction like that?

I have found the way. I now managed to make a script that does exactly what I want it to: Align the view to the middle or bottom (or it can be aligned to anywhere from top to bottom). The script expands the camera's Up limit when the Window size exceeds the camera's Bottom limit, so it only works when the camera's Default up limit is 0, for exactly this reason.

The script needs to be attached to the camera you want to realign. Or it can be attached to something else if you change the extends and set the camera variable accordingly.

extends Camera2D



func _process(_delta):
		align_view()



onready var camera = get_node(".")

export (float) var cam_extension_direction_y = -0.5
	# -1 = Top
	# >-1 and <-0.5 = Range between Top and Middle
	# -0.5 = Middle
	# >-0.5 and <0 = Range between Middle and Bottom
	# 0 (Or better remove the script) = Bottom

func align_view():	if get_viewport_rect().size.y > camera.limit_bottom:
	camera.limit_top = ( get_viewport_rect().size.y- camera.limit_bottom ) * cam_extension_direction_y
2 years later

I believe I've solved this problem by customizing the Root Node of my Scene.

The pre-requisites here are that your project settings are set to:

  • Display/Window/Stretch/Mode: 2d
  • Display/Window/Stretch/Aspect: Expand

Then you'll need to make the Root Node in your Scene a "Control" type node.

At the top of the 2D editor, with the Root Node selected, click Layout:

  • First select "Full Rect"
  • Open menu again and select "Center"

There are some other options in there for you to play with as well, to get the expansion to happen differently