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)) {


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



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?


1 Answer 1


Why don't you make the pillars children of some parent object and move that instead. You can use Transform.SetParent to achieve this, and then you only need to move the parent and the distance between the pillars will stay the same.

You can use the following to control the acceleration.

float addingrate = 0.5F;
float nextadd = 0.0F;     

if (Time.time > nextadd) 
    nextadd = Time.time + addingrate;
    bin.GetComponent<Rigidbody2D>().velocity += tempvel;
  • \$\begingroup\$ Problem is, the parent will keep going. It might cause problems when it's position goes to something like (-10,000f,0) \$\endgroup\$
    – SanSolo
    May 7, 2015 at 14:09
  • 1
    \$\begingroup\$ Hmmm or i could reset position of parent once it has reached a certain point. I'll try! \$\endgroup\$
    – SanSolo
    May 7, 2015 at 14:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .