- Home /
how to get the lowest right corner of the camera(and highest left) in units?
hey guys i need help,im making a 2d procedurally generated sidescroller game and i want my "world" to be created from the lowest point of the camera to the highest point of the camera divided by 0.64 units cubed tiles and when i try Camera.main.Rect and use the minX to get the lowest point i get 0 and it shouldnt give me 0 because it is not 0..can anyone help with that please?
WHAT IT DOES: it loops on i as the index for horizontal block j is the index for vertical block the place to start the generation of blocks is supposed to be from the camera's lowest y point. the blocks are 0.64 units each but something was wrong with my calculation and it was rendered better if i calculated the space between each block as index times 0.62+offset that i tried getting and if i were to to index times 0.64 it would render it with black lines between my sprites.. if theres a fix for that too id really appreciate that. thank you for the solvers :)
my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class terrainGenScript : MonoBehaviour
{
public GameObject block;
public GameObject dirtTop;
public GameObject dirtMid;
public GameObject dirtBot;
public GameObject knife;
public double offset;
public Camera cam;
public Canvas canvas;
public Text myText;
// Use this for initialization
void Start()
{
offset = 0.016;
}
// Update is called once per frame
void Update()
{
}
public void Gen()
{
//sprite pixels / 100 and units in import setting have to be 100
double sizeOfblock = 0.62;
Rect r = Camera.main.rect;
//number of blocks fitting in the height of the cam
float numOfFittingBlocks = (Camera.main.orthographicSize*2) / 0.64f;
int numOfBlocksHeight = (int)(numOfFittingBlocks);
if (numOfFittingBlocks - numOfBlocksHeight >= 0.5)
numOfBlocksHeight++;
//number of blocks on the x axis
int numOfblocks = Random.Range(64, 120);
Debug.Log(numOfblocks+"numOfBlocks on x axis");
//just
//int numOfBlocksHeight = (int)(numOfFittingBlocks);
//2d array represents actual world blocks-the world itself ingame
GameObject[,] arrayOfblocks = new GameObject[numOfBlocksHeight, numOfblocks];
//Vector2 firstPos = new Vector2(0f, 4.3f);
//array of block types
GameObject[] blockTypes = new GameObject[5];
//assigning
blockTypes[0] = dirtTop;
blockTypes[1] = dirtMid;
blockTypes[2] = dirtBot;
blockTypes[3] = knife;
blockTypes[4] = block;
////////////////////////////////////////////////////////////////////
r = Camera.main.rect;
float camerasLowestY = r.yMin;
///////////////////////////////////////////////////
myText.text = "r.Height="+r.height+"lowest y"+camerasLowestY+ "numOfBlocksHeight on y asix"+numOfBlocksHeight;
Debug.Log(numOfBlocksHeight+"numOfBlocksHeight on y axis");
//actual building
for (int i = 0; i < numOfBlocksHeight; i++)
{
for (int j = 0; j < numOfblocks; j++)
{
if(i<3 || i> numOfBlocksHeight-4)
arrayOfblocks[i, j] = (GameObject)(Instantiate(blockTypes[1], new Vector3((float)((j * (sizeOfblock+offset))), (float)((camerasLowestY + i * (sizeOfblock + offset))), 0f), new Quaternion(0, 0, 0, 0)));
}
}
}
public float GetScale(GameObject t)
{
float locs = t.GetComponent<Transform>().localScale.x;
Debug.Log((double)(locs)+"scale");
return locs;
}
}
alright thank you ill try to rephrase that basiclly i have two loops one that runs on the vertical blocks and the other on vertical,basiclly i want to save memory by making the map only generate at the start of the game from the bottom of the camera up and just not more than that,because of that i want to get the screen height and count the number of blocks that can fit on screen which are the size of 0.64 units so i want to calculate screen height/0.64. and when i tried the methods above it didn't really work... its kinda hard to explain but thank you i think the links would help a lot. thank you :).
just checked viewPortToWorldPoint and thats exactly what i need thank you so much!
Nice ;) I have switched my comment to an answer. Please, click on the check mark under the vote button to accept the answer. Somebody could face a similar problem in the future.
Answer by Hellium · Jun 26, 2015 at 06:28 PM
"the lowest point of the camera to the highest point of the camera divided by 0.64 units"
This doesn't mean anything.
I don't really understand what is your question.
The Screen class can give you the size of your screen http://docs.unity3d.com/ScriptReference/Screen.html
Camera.main.ViewportToWorldPoint can give you a 3D/2D position from a point in the view port (between 0 and 1), and Camera.main.ScreenToWorldPoint does the same but with a screen space point (between 0 and Screen.widt or Screen.height) http://docs.unity3d.com/ScriptReference/Camera.html
Your answer
Follow this Question
Related Questions
2.5D Platformer Camera follow character Y axis but not for every jump? 1 Answer
Position Gui Text Universally 1 Answer
Diagonal camera limit 0 Answers
2D Animation does not start 1 Answer
Passing Underneath Section of a Mesh 0 Answers