How to make correct position?

4.3

Even if the viewport width is set to 1280x720, the postion value of the sprite or border is about 2.5 times lower than the position I want. For example, if you set the viewport to 1280 and set the position of a sprite to 640 in the middle, nothing appears on the game screen and it is somewhere around 1600 on the right.

It sounds like your viewport is not where you expect it to be. This could be for a variety of reasons including your original camera position settings, your scaling, zoom, window settings or even how you are positioning your sprite with respect to parents etc.

Try using this function to get the position of the top left and bottom right of your viewport in global world co-ordinates and see if they are what you were expecting.


func print_viewport_corners():
	var viewport = get_viewport()
	var canvas_transform = viewport.get_canvas_transform()
	var viewport_size = Vector2(viewport.size)
	
	var top_left = canvas_transform.affine_inverse().origin
	var bottom_right = canvas_transform.affine_inverse() * viewport_size
	
	print("Top left corner (world coordinates):", top_left)
	print("Bottom right corner (world coordinates):", bottom_right)

Hope that helps.

1 Like