- Home /
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! :)
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);
}
}
}
Answer by ByteSheep · Apr 21, 2012 at 10:34 PM
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 ;)
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++;
}
That works perfectly! I knew it was simple, thank you so much. :)
Hello,
I'm really new at scripting and to Unity itself. Doing some research to work on a game similar to this tunnel effect that looks like as if a character is falling through a hole.
I'd like to ask how does the NextCreate help with this situation because I'm stuck at understanding that part. And also why the value of 47.87?
Hopefully someone turns up to answer because this is an old thread. :)
Edited my answer with a script that uses coroutines ins$$anonymous$$d of Time. Also added a couple comments to hopefully make it more clear what each line is doing.
Answer by Atrius · Apr 21, 2012 at 10:08 PM
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);
}
}
Yeah, I'm not too good with C#. I learned Java/UnityScript. That script makes some sense, though. And it does have some errors but I get the gist of things. Still not exactly sure how I should input my own values into that, though. I edited my question with the current script I was using if you want to take a look at it.
I'll figure it out eventually, so you don't have to stress yourself with this. :)
Also, I worked things out and the one error I get from your script is this: Assets/Scripts/InstantiateC.cs(14,26): error CS1061: Type UnityEngine.Transform' does not contain a definition for
translate' and no extension method translate' of type
UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)
I would like to see how yours works ingame and I can probably convert it and tweak it.
I believe the root cause of the issue is Translate is capitalized. I adapted this from memory off a system I had used but for $$anonymous$$e I was storing a reference to each piece. Your code is much cleaner assu$$anonymous$$g you don't need the reference and just need it loaded in the world.