infinite BG moving towards the character.

I'm trying to create a game with a moving BG which keeps moving towards the character. I currently have 2 boxes which shares the same script below to give it an illusion that the character is running in an infinite map.

var BGSpeed = 100.0;

    function Update() 
    {
        if (transform.position.z <= -30) //if the bg geo passes the character, it'll go back to the beginning.
    {

            transform.position.z=69.45;
            Spawn();

    }
        else
        {
// Move the object forward along its z axis 1 unit/second * BGSpeed.
            transform.Translate(Vector3.forward * Time.deltaTime * -BGSpeed );
        }

    }

The problem I'm having with this script is that it's very unstable. it seems like time.deltaTime is not acurate enough and I can see the seam between the two boxes.If I run the game long enough, sometimes the gap between the two boxes get so big, the character fall through..

Any ideas? Thanks in advance,

The issue may be that the slight difference defined by being less than or equal to 30 accumulates over time. Say you move over 30 by .5 units. When you reset it is off by .5 units. Perhaps find the actual over or undershoot when you reset the box and add or subtract the amount to take care of any offset occurring.

I think that the key is not to work with moving your object to a set point, but to an offset that compensates. This negates innacuracy . Here's my solution to a 3D tiling infinite landscape, and it works very well...

public static Vector3 RepeatPos (Vector3 nPos){ Vector3 vPos=nPos; if(gTiling){ if(gGoody!=null){ Vector3 vDelta=vPos-goodyScript.GetPos(); if (vDelta.x>gMaxDist){ vPos.x-=gMazWid; }else if(vDelta.x<-gMaxDist){ vPos.x+=gMazWid; } if (vDelta.z>gMaxDist){ vPos.z-=gMazWid; }else if(vDelta.z<-gMaxDist){ vPos.z+=gMazWid; } } } return (vPos); }

This is called by any object that needs to tile, and does not reference time at all. note: gmazwid is just a number and gmaxdist is exactly half, so it basically moves any object that you've gone a certain distance away to the other side in 3D space.

-----further notes.....

As a note: the original question sounds like all that is needed is minor changes as hinted above... The solution posted is an example of tiling that looks ok from all angles, not just firing down one given axis, so... simpler solution for 2D tiling:

if(transform.position.x<=0){ transform.position.x+=gMaxDist; }

instead of "transform.position.x=gMaxDist;" which would result in gaps and offsets.

the other code above does work, but depends on all the above variables being set and accessible, and is "fully 3D".

firstly this behaviour should belong somewhere like in a "gamescript", as if you use it, you'll use it a hell of a lot.

try replacing my "goodyScript.GetPos()" with the goody's transform.position somehow (that's all "GetPos()" returns from the goodyscript), you could have a static "Pos" variable in the goody.

You'll also need to set your gMazwid and gMaxDist (gMazWid is the entire size of your level, and gMaxDist is half of gMazWid (only there to save on typing).

Your tiling objects will need the following code in the Udate function. transform.position=WhatEverMyScrtiptIsCalled.RepeatPos(transform.position);