0 votes

I am trying to spawn enemies at the edge of the camera. But I do not know how to get the edges of the camera as positions. I can get the middle of the screen using Camera.GetCameraScreenCenter() and the size of the camera viewport with Camera.GetViewport().Size. But since the viewport is in pixels, I do not know how to offset the screen center by 2D units.

Godot version 3.5.1
in Engine by (25 points)

I think when working in 2d the pixels are the units, unless you have done scaling to your nodes.

@magicalogic using your insight, I was able to solve this. If you write your comment as a post, I can mark it as best answer.

3 Answers

0 votes

just add / subtract half the size to the center to get the edges.

by (8,548 points)
0 votes

The below code is how I achieved computing camera center and edge position calculations, maybe it can help you:



func computeScreenCenter():
var minPos = computeMinimumPointBoundary()
var maxPos = computeMaximumPointBoundary()
var screenRect = Rect2(minPos,Vector2())
screenRect = screenRect.expand(maxPos)
return calculate_center(screenRect)
func computeMinimumPointBoundary():
# Get view rectangle
var ctrans = getcanvastransform()
var minpos = (-ctrans.getorigin() / ctrans.getscale())
return min
pos func computeMaximumPointBoundary():
# Get view rectangle
var ctrans = getcanvastransform()
var minpos = -ctrans.getorigin() / ctrans.getscale()
var view
size = getviewportrect().size / ctrans.getscale()
var max
pos = minpos + viewsize
return max_pos func calculate_center(rect):
return Vector2(
rect.position.x + rect.size.x/2,
rect.position.y + rect.size.y/2)
by (1,080 points)
0 votes

When working in 2d the pixels are the units, unless you have done scaling to your nodes.

by (2,017 points)
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 Frequently asked questions and 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 [email protected] with your username.