I'm building an infinite runner and have to generate pillars on which the player jumps. There's a minimum and maximum value for how much gap can exist between two pillars. In addition to this, i want the pillars to move to the left with increasing velocity. All pillars need to have same velocity. So, i made a pillarHolder object which would have velocity that increases with time. The instantiated pillars would be made children of this pillarHolder. Code is:
if ((Vector3.Distance (go.transform.position,transform.position)<25f)) {
t=rand.Next(13,35);
t=t/10;
//Debug.Log(t);
tempx+=t;
pos=new Vector2(tempx,-2.22f);
go=Instantiate(pillar,pos,Quaternion.identity) as GameObject;
go.GetComponent<Transform>().parent=holder;
}
holder.GetComponent<Rigidbody2D> ().velocity=new Vector2(-10f,0);
The problem is, the pillarHolder keeps moving in the negative x direction and after a while, the pillars are not positioned properly. The gap between them exceeds the maxGap. I tried to solve the problem by resetting the pillarHolder's position once it went beyond position.x=-20f. But when it's position resets, child objects are reset too.
Any help please?