Making an endless tunnel

Hi again!

Right now I’m trying to make an endless tunnel. I have say about 5 prefabs stuck together to make a tunnel at the beginning of the level, for a start. When you get close to the end of this pre-done set of tunnels, I want it to create say 5 or 10 more after those. (They’re on the Z axis)

I’ve been messing around with the various Instantiate functions, and I know that’s what I need to do but it’s just not working the way I want it to.

I was using the first Instantiate script from Unity’s Instantiate scripting page and it’s almost exactly what I want. It creates them and I can specify how many, and how spaced apart they are on what axis. Got all that working, set up a timer to make it only instantiate 10 every 10 seconds, but the problem is, it’s creating the same 10 over each other every time. It doesn’t know that I want them created further along the Z axis, from the last batch. Is there an easy way to do this? Still not a whiz at scripting so any help would be appreciated.
Thanks! :slight_smile:

EDIT: My current script:

var prefab : Transform;

private var CreateTimer = 10.0;
private var NextCreate = 0.0;

function Update() 
{

  if(Time.time > CreateTimer + NextCreate)
  {
    NextCreate = Time.time;
    for (var i : int = 0;i < 10; i++) 
    {
      Instantiate (prefab, Vector3(0, 0, i * 47.87), Quaternion.identity);
    }

  }

}

You need to instantiate the prefab as a GameObject and then translate its position to move it. Without seeing your existing script I can’t help modify yours. Here’s a C# script I just wrote up, there could be syntax issues I didn’t actually compile this, but I hope it gives you an idea.

public class TunnelGenerator : MonoBehavior {
	int segmentNumber = 0;
	float segmentLength = 15.0f;

	void CreateSegment() {
		// Increment what segment number you are on
		segmentNumber++;
		// Create a vector with your X,Y static and your Z the length of the prefab times the number of segments
		Vector3 pos = new Vector3(0.0f,0.0f,segmentLength * segmentNumber);
		// Instantiate it
		GameObject segment = (GameObject)Instantiate.(Resources.Load("TunnelPiece"));
		// Move it into place
		segment.transform.Translate(v, Space.World);
	}
}

If I understand correctly, then this should do the trick:

var prefab : Transform;

private var CreateTimer = 10.0;
private var NextCreate = 0.0;
private var count = 0;

function Update() 
{

  if(Time.time > CreateTimer + NextCreate)
  {
    NextCreate = Time.time;

    for (var i : int = 0;i < 10; i++) 
    {

      count++;

      Instantiate (prefab, Vector3(0, 0, count * 47.87), Quaternion.identity);
    }

  }

}

Basically you need a variable that won’t be reset each time the ten pieces have been created… Hope this helps :wink:

EDIT:
Here’s a slightly cleaner version using coroutines (untested).

var prefab : GameObject;
var segmentPrefabCount = 10;
var pieceLength = 20.0f;
var startPositionOffset = Vector3(0, 0, 0);
private var pieceNumber = 0;


function Start() 
{
  // Create a new segment every 10 seconds
  InvokeRepeating ("CreateSegment", 10f, 10f);
}


function CreateSegment()
{
  // Create a segment with segmentPrefabCount amount of prefabs
  for (var i = 0; i < segmentPrefabCount; i++)
  {
    CreatePiece();
  }
}

function CreatePiece()
{ 
  // Position for this piece will be the startPositionOffset plus the length of a prefab times it's index
  // Change Vector3.forward to whichever direction you need (e.g. -Vector3.up)
  var segmentPosition = startPositionOffset + Vector3.forward * (pieceNumber * pieceLength);
  Instantiate (prefab, segmentPosition, Quaternion.identity);
  pieceNumber++;
}