0 votes


I have some problems with my game because of position of something like background and spawn enemies.

I make a code to always show background on full screen to don't show that gray mark.

On macbook i get this:

And on tablet i get this:

And i think the same problem happen on game, because on macbook if i don't resize if place correctly, but if i resize it place it wrong.

On macbook i get this:

And on tablet i get this:

I think that all problems are related to wrong calc. My function to calc background position:

extends Node2D

func _ready():
    var viewportWidth = get_viewport().size.x
    var viewportHeight = get_viewport().size.y

    var scale = viewportWidth / $Background.texture.get_size().y

    $Background.set_position(Vector2(viewportWidth/2, viewportHeight/2))
    $Background.set_scale(Vector2(scale, scale))

    print("[MainMenu] Viewport size: ", viewportWidth, "/", viewportHeight)

func _on_ButtonStart_pressed():

AND now my code in game to position my enemy spawn:

func spawn():
    var spawn = enemy1.instance()
    var vp_size = get_viewport().size
    var spawn_size = spawn.get_node("Sprite").frames.get_frame("normal", 0).get_size()
    var pos_x = (spawn_size.y / 2) + vp_size.x
    var pos_y = rand_range((spawn_size.y / 2), (vp_size.y - (spawn_size.y / 2)))
    spawn.position = Vector2(pos_x, pos_y)

    print("[Game] Spawn at: ", pos_x, "/", pos_y)

Can anyone help me understand it?

Im using latest version of godot.

in Engine by (18 points)

I have published my game with this problem. If anyone can help me will be nice.

Download here for iOS and Android:


1 Answer

+1 vote

I would try using the 2d stretch mode and expand stretch aspect as explained in the Multiple resolutions documentation. Once these options are selected, you don't have to do anything specific to support multiple resolutions anymore.

by (11,295 points)


Thanks, but i already use it. I tried fixed height and now expand.

See the screenshot on iphone, ipad and android:

Obs: The black part is the notch for camera, so ignore it.

And now on macos:

The strange is that players and items are correctly positioned.

My code of background position:

extends Node2D

func _ready():
    var viewportWidth = get_node("/root/World").get_viewport_rect().size.x
    var viewportHeight = get_node("/root/World").get_viewport_rect().size.y

    var scale = viewportWidth / $Background.texture.get_size().y

    $Background.set_position(Vector2(viewportWidth/2, viewportHeight/2))
    $Background.set_scale(Vector2(scale, scale))

    print("[MainMenu] Viewport size: ", viewportWidth, "/", viewportHeight)

Did you understand what can be wrong?


Now the problem can be understand better.

There is a margin on left size that put all to right, but the controls still centralized.

See here:

enter image description here

Hello , I also faced the exact same problem with my game..it works fine on mobile devices. But when i play it on a tablet i also get the same problem. I'm guessing you are using camera 2D nod?.. Remove the camera nod and try, it should be centered as you expected. I think it could be a Bug of camera nod. But still i also have no solution to the problem, as you did i also might publish the game like this. But if there is a way of move screen without the camera nod. I think you can find a solution. Also i have different scenes with bg images, all of them are centered correctly. only one i cannot centered is the one with camera 2d nod. If you found any solution please let me know. Thank You

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.