private void AddBranches(GameObject tree)
{
int branchAmt = Random.Range(1, 5);
spawnedBranches = new GameObject[branchAmt];
for (int j = 0; j < branchAmt; ++j)
{
int branchType = Random.Range(0, branches.Length);
float branchRotation = Random.Range(-75f, 75f);
float branchPosX = Random.Range(tree.transform.Find("left").position.x, tree.transform.Find("right").position.x);
float branchPosY = Random.Range(tree.transform.Find("top").position.y, tree.transform.Find("bottom").position.y);
GameObject branch = Instantiate(branches[branchType], new Vector3(branchPosX, branchPosY, 0), Quaternion.identity); //tree.transform
float branchOverHalf = tree.transform.GetChild(1).position.x + ((tree.transform.GetChild(2).position.x - tree.transform.GetChild(1).position.x) / 2);
float placeDiff = branch.transform.Find("attachHere").position.x - branch.transform.position.x;
Vector3 placeBranch = branch.transform.position;
if (branch.transform.position.x > branchOverHalf)
{
Vector3 flipBranch = branch.transform.localScale;
placeBranch.x += placeDiff;
branch.transform.position = placeBranch;
flipBranch.x *= -1;
branch.transform.localScale = flipBranch;
}
else
{
placeBranch.x -= placeDiff;
branch.transform.position = placeBranch;
}
spawnedBranches[j] = branch;
AddLeavesToBranches(branch);
}
Branch branchRef;
for (int i = 0; i < spawnedBranches.Length; i++)
{
if (spawnedBranches[i] != null && i == 1)
{
//branchRef = null;
//Random.InitState(23237);
branchRef = spawnedBranches[i].GetComponent();
branchRef.itemWithin = pickupItems[Random.Range(0, 11)];
}
else
{
//branchRef = null;
//Random.InitState(23237);
branchRef = spawnedBranches[i].GetComponent();
branchRef.itemWithin = pickupItems[Random.Range(0, 11)];
}
}
}
Leave a Reply