- Home /
What is the best way to use the bounds of the camera in-world effectively?
Okay, admittedly this question is two-fold. First, I'd like to get the bounds of the camera for the width and height, in order to tile a background map in order to fit inside a defined area. The camera itself is static and does not move, the game is single-screen.
I've been trying to use Screen.width/Screen.height, however that results in upwards of a thousand tiles. I know I also need to get the 'real' size of the tiles as well, which are gameobjects - a custom 4-vertex plane.
Secondly, I'd like to be able to get the world bounds of the camera in order to actively place objects inside it.
Can anyone offer any suggestions? I'm already using ScreenToWorldPoint to get the edge of the viewport.
Is your camera orthographic or perspective? Is it possible to get a screenshot of what you're trying to achieve?
Have you tried setting the tiling manually from the material inspector?
I've tried setting it manually but there is no guarantee that the manual version will stretch to the edge of all resolutions. The map will currently always start at 0, screen.height, however.
The camera is orthographic
Answer by robertbu · Aug 22, 2013 at 02:36 PM
The size parameter (Camera.orthographicSize) is 1/2 of the vertical height seen by the camera in world units. So:
VerticalHightSeen = Camera.main.orthographicSize * 2.0;
HorizontalHeightSeen = VerticalHeightSeen * Screen.width / Screen.height;
I've actually already tried something like that, it didn't really work. I -think- what I want is Screen.width, but I keep getting ridiculously high values for the tiling limits with that.
For an Orthographic camera, the values are correct for the world units it sees. To verify, do the following:
Start a new scene
$$anonymous$$ove the camera's 'y' position to 0. The camera should be at (0,0,10).
Set the camera to Orthographic.
Put a cube in the scene at the origin.
Size the cube to (10,10,1).
Set the Orthographic size to 5.
You will see the cube's height exactly fills the frame.
Screen.width and Screen.height is the size of the screen in pixels.
Can you post your tiling code?
void buildGround () {
GameObject groundBase = new GameObject("BackgroundGround");
for (int y = 0; y < gridY; y++) {
for (int x = 0; x < gridX; x++) {
Vector3 pos = new Vector3(x, -y, 0) * spacing;
Quaternion rot = new Quaternion(0,90,90,0);
GameObject groundpiece = Instantiate(groundBackground, pos, rot) as GameObject;
groundpiece.transform.parent = groundBase.transform;
}
}
Vector3 screenPoint = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height, 10));
groundBase.transform.position = screenPoint;
}
Once I've got my screen width/height, the idea will be to use the size of groundBackground, and divide in order to get a tiling limit that fits within the screen area, effectively dynamically altering gridY / gridX
Some comments:
You should not build a Quaternion directly. The values are are not degrees. You should do:
Quaternion rot = new Quaternion.Euler(0,90,90);
The rotation makes me think you are trying to tile a built in plane. Note their default size is 10 x 10.
Nothing here is scaling the game object that you place.
I don't understanding what you are doing with the last two lines.
Here is a bit of code that tiles a sphere across the visible area:
using UnityEngine;
using System.Collections;
public class Bug20 : $$anonymous$$onoBehaviour {
void Start() {
TileWithSpheres (12,9);
}
void TileWithSpheres(int numX, int numY) {
float verticalSeen = Camera.main.orthographicSize * 2.0f;
float horizontalSeen = verticalSeen * Screen.width / Screen.height;
float gridX = horizontalSeen / numX;
float gridY = verticalSeen / numY;
Vector3 v3Pos = new Vector3(-gridX * (numX - 1) / 2.0f, -(gridY)*(numY - 1) / 2.0f, 0.0f);
for (int i = 0; i < numY; i++) {
for (int j = 0; j < numX; j++) {
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.transform.position = v3Pos;
go.transform.localScale = new Vector3(gridX, gridY, 1.0f);
v3Pos.x += gridX;
}
v3Pos.y += gridY;
v3Pos.x = -gridX * (numX - 1) / 2.0f;
}
}
}
Yes, I am creating a plane with tiled objects. The primitive is a custom four-vertex plane. I did not use the default on purpose. The last two lines ensure that it is always starting at the top-left edge of the visible area. I've fixed the .euler, thanks. Nothing is scaling because the object itself is scaled, although scaling it via code may be more effective. They need to be scaled in order to fit an area they can be 'knocked out' in, IE - clicked on and they vanish.
Your answer
Follow this Question
Related Questions
Camera Bounds 2 Answers
Camera Viewport Height - Horizontal Transition 0 Answers
Camera: objects shown multiple times at once 1 Answer
Stretch Camera viewport to fit the window [Pixelated] 1 Answer