Trying to spawn boxes just outside the cameras viewing rect

Started by
9 comments, last by BiiXteR 7 years, 5 months ago

I'm trying to spawn a bunch of boxes at random positions between the camera rect's minimum x and minimum y point, and slightly over the cameras Y point.

But I have no idea at all how I should go about calculating where those points are in world space.

Here's a picture of the points I want to get, the rectangle is the cameras viewing rect.

4bOcGJk.png

Also, if you didn't read the tags, I'm using Unity for this project, and it's a 2D (prtographic) camera.

Advertisement
At what depth?

You'll need the details of your viewport and the near and far depth to calculate where the frustum corners are. Unity may already have this information somewhere.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
He mentioned he is using an orthographic camera, so the depth shouldn't be a problem.

Take a look at the (orthographic) size of the camera. This value is half of what the camera whill capture on the vertical axis. If you add/subtract it to/from the y-position of the camera, you'll get the upper/lower edge. To get the left/right edge, you'll have to multiply it with the cameras espect ratio, and add/subtract the result to/from the x value. The property "aspect" should be the one you can use for that, otherwise you can calculate it by deviding the viewport width by the viewport height.
After you got the the corner, you'll need to add/subtract half the objects size you want to spawn in order for it to be just outside the cameras view.
(All this assumes your camera is not rotated. If it's rotated in 90° steps, you'll just need to swap some angles or invert some values. If it is another angle, you'll need to do some more math, but the basic calculation should be the same.)

Btw: The C# tag is visible in the topic overview, the Unity tag is not. You might want to change the order of the tags, since Unity is the most important information in this case.

Ah, I missed the last part for some reason.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

He mentioned he is using an orthographic camera, so the depth shouldn't be a problem.

Take a look at the (orthographic) size of the camera. This value is half of what the camera whill capture on the vertical axis. If you add/subtract it to/from the y-position of the camera, you'll get the upper/lower edge. To get the left/right edge, you'll have to multiply it with the cameras espect ratio, and add/subtract the result to/from the x value. The property "aspect" should be the one you can use for that, otherwise you can calculate it by deviding the viewport width by the viewport height.
After you got the the corner, you'll need to add/subtract half the objects size you want to spawn in order for it to be just outside the cameras view.
(All this assumes your camera is not rotated. If it's rotated in 90° steps, you'll just need to swap some angles or invert some values. If it is another angle, you'll need to do some more math, but the basic calculation should be the same.)

Btw: The C# tag is visible in the topic overview, the Unity tag is not. You might want to change the order of the tags, since Unity is the most important information in this case.

Fixed the tags, though I don't understand why you need to multiply with the aspect ratio? :o

The value stored in orthographic size is just the "vertical size". Since the camera is most certainly not showing a perfectly rectengular view of the game, the "horizontal size" is a different value. You can calculate that by horizontal size = vertical size / height * width. Since the aspect ratio is already calculated with a aspect ratio = width / height, you can shorten the calculation to just horizontal size = vertical size * aspect ratio

The value stored in orthographic size is just the "vertical size". Since the camera is most certainly not showing a perfectly rectengular view of the game, the "horizontal size" is a different value. You can calculate that by horizontal size = vertical size / height * width. Since the aspect ratio is already calculated with a aspect ratio = width / height, you can shorten the calculation to just horizontal size = vertical size * aspect ratio

Oh, I see.

Managed to get the left and right sides working, however I'm still having problems with the upper side of the camera.

Here's my current code which results in the objects spawning too much over the upper side of the camera rect :


objectY = Camera.main.transform.position.y + Camera.main.orthographicSize;

Camera.main.transform.position.y might be the top edge, so you might just be able to spawn the object there (or ObjectHeight above).

Hello to all my stalkers.

Camera.main.transform.position.y might be the top edge, so you might just be able to spawn the object there (or ObjectHeight above).

Yeah, that worked, thanks :)

Camera.main.transform.position.y might be the top edge, so you might just be able to spawn the object there (or ObjectHeight above).


Yeah, that worked, thanks :)


No, that value is not the top edge of the camera, but it is its center. If it results in the desired behavior, the problem lies at another place. The camera in use might not be the main camera, resulting in Camera.main returning the wrong camera. The spawned object might not be "centered" properly, meaning its position is not in its center. Without further knowledge about the GameObjects in the scene and the other code, I can't say with certainty what the reason for this actually is.

Btw: you're better of not using Camera.main, since it probably makes a search for the proper camera by the Tag "MainCamera" every time you want to retrieve the camera. Depending on how often you use it, this could affect the performance of the game. Also, by not searching for the object you want to use, but by getting it assigned from outside, your code is getting rid of dependencies. (See "inversion of control".)

This topic is closed to new replies.

Advertisement