I'm creating an infinite runner where character has to jump on randomly generated pillars. The pillars are positioned with a gap between each of them. The gaps are dmin and dmax. In addition to this, i want the pillars to move to the left of screen with increasing velocity. But they all need to move with same velocity so that the gap in between them is maintained. I have the script as follows: pillar is a prefab.go is an empty gameobject which will later be used to Instantiate pillars. first is the first pillar, manually positioned in the scene.
using UnityEngine;
using System.Collections;
public class GM : MonoBehaviour {
float tempx,dmin=1.3f,dmax=3.5f;
GameObject[] bins;
Vector2 speed,pos,tempvel;
public GameObject pillar,go;
public Transform first;
// Use this for initialization
void Start () {
speed = new Vector2 (-0.005f,0);
tempx = first.transform.position.x;
tempvel = new Vector2 (0, 0);
}
void Update () {
if ((Vector3.Distance (go.transform.position,transform.position)<40f)) {
tempx+=Random.Range(dmin,dmax);
pos=new Vector2(tempx,-2.22f);
go=Instantiate(pillar,pos,Quaternion.identity) as GameObject;
}
bins = GameObject.FindGameObjectsWithTag("trash");
tempvel += new Vector2 (-0.0005f, 0);
foreach (GameObject bin in bins) {
//bin.GetComponent<Rigidbody2D>().velocity+=new Vector2(Random.Range(-0.005f,-0.05f),0);
bin.GetComponent<Rigidbody2D>().velocity+=tempvel;
}
}
}
Problem is, the speed of pillars increases way too fast. They move to the left at nearly invisible speeds. Also, after a certain speed, the gap between the pillars increases. They become so far apart, one is in camera view, another is somewhere far away.
Is there a way to accomplish this?