2D Course - Laser Defender - Objects Rendering Outside of Camera Bounds

Hey All!

I’m working through the 2D tutorials and ran into an interesting problem (? or expected?), where the 9:16 resolution looks amazing in Unity, but once I build the program, I find objects rendered outside the camera area. i.e. game objects are rendered outside the play area, and since I’m using gameCamera.ViewportToWorldPoint(), even the clamping on the spaceship is failing – the user can move the space ship outside the play screen and survive indefinitely.
I’ve attached a screenshot for reference.

I made a workaround for this by adding a black images to the Canvas to prevent sprites visually appearing in these areas (&& defining the game units directly in my movement scripts to prevent the user from going out of bounds), but I can’t help but won

der – is there a better way to do this?

Or more directly: how do you:
A) set the xMin / xMax to the playable bounds instead of window bounds (which are defined by the user’s monitor/resolution)
B) prevent objects from rendering outside the camera / play area
?

Any input/thoughts appreciated.

By the way, I’m taking a week off work to run through this course, and so far, what a blast! Thanks Rick and team – this is seriously impressive content, having so much fun.

Hi Graeme,

Welcome to our community! We are glad to hear that you are enjoying this course. :slight_smile:

Our game was designed for a specific resolution or aspect ratio: 9:16. This resolution or aspect ratio must be set in File > Build Settings > Player Settings. Otherwise, the game area might be larger and display sprites that are “outside” the game window in Unity.

In the Glitch Garden section, Rick teaches how to design games for multiple aspect ratios.

Regarding your questions:

  1. If you want to restrict the player’s movement to the edges of the Quad, you could go that by using the quad’s position and its scale value to determine the position of its edges.

  2. Actually, the yellow box is not quite right in your screenshot. It is covering the entire game screen because it is the only camera in your scene. Sprites get rendered by a camera. To make your life easy, you could add a SpriteRenderer game object on the left and right side of the background. Set its z-position to -1 so it is closer to the camera and make the sprite black. This way, it will cover the areas, and the sprites that are outside the yellow box will not be visible.

Did this help? :slight_smile:


See also:

Think I follow what’s going on.

If I switch to Windowed mode in player settings and manually input a fixed 9:16 resolution I don’t have the issue – i.e. only the game screen + camera view get rendered. It’s only an issue for full screen, where the playscreen expands to the entire monitor, but the game content is fixed. (from what I can see) I don’t see a ‘clean’ fix to the issue in settings alone.

Anyway,
1 - that’s a nice idea! definitely, using this
2 - this ended up being what I did for fullscreen use – the question remained “how big do the black bars need to be if content can get pushed left/right?”,
which I guess just translates into “how wide can a monitor get?”

– anyway, I just made them really wide :smiley:

Thanks Nina!

Depending on what you define as “clean” or what your desired result is, you might have to write a class that arranges the game objects during runtime depending on, for example, the aspect ratio. That’s not difficult, just a lot of work.

Make them as big as it makes sense. The best would be to test different aspect ratios in the game window, for instance, 16:9.

That’s true. As a game developer, you would have to do some research, so you don’t waste your time supporting irrelevant aspect ratios.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms