- Home /
seperation of prefabs with two dimensional terrain generation
I have the following function:
public void Spawn ()
{
GameObject g = ((GameObject)Instantiate ( hills [Random.Range (0, hills.Length)], new Vector3((subs[subs.Count-2].transform.position.x+(subs[subs.Count-1].renderer.bounds.size.x/2)+(subs[subs.Count-2].renderer.bounds.size.x)/2),transform.position.y,transform.position.z ), Quaternion.identity));
g.transform.parent = this.transform;
NonLoopingScrollScript nls = (NonLoopingScrollScript) g.GetComponent("NonLoopingScrollScript");
nls.Setup(movSpeed,0.0f);
subs.Add (g);
}
Which is being used to spawn terrain, the way this works is I have an array(hills) which is composed of a bunch of prefabs which together will make up terrain in the background of the game. This function is what actually instantiates the next prefab when it is needed, this prefab is then added to a list (subs). My problem is simply the spacing between the prefabs as I would like there to be no space, yet sometimes the objects overlap or there is a gap between the prefabs.
Explanation of code: In the first line I instantiate a random prefab with the argument:
hills [Random.Range (0, hills.Length)]
The next argument is where I am having trouble, what I am currently doing is setting the x value of the new prefab to the x value of the transform plus half the x width of both the previous prefab, and the new prefab. This is the issue as the spacing is not as desired. Then i simply set the y and z value to that of the spawner object and the rest of the code is irrelevent, save for where at the end of the function I then add the new gameobject to the end of the list.
I hope this was clear, if not please do not hesitate to ask questions.
You formatted your code with the quotation button. This makes the code unreadable. Please edit your question, delete your current code, past the code in again, and use the 101/010 button to format the code.
Your answer
Follow this Question
Related Questions
Transform.position not working ??? 0 Answers
Convert terrain map coordinates (GetDetailLayer) into world position 0 Answers
How Do I Align Game Objects Evenly In 2D Space? 0 Answers
Object with many children will not show up in the center although its coordinates are set to 0, 0, 0 1 Answer
transform getting altered by changing prefab settings 1 Answer