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;
}
}