How to generate a random terrain (like temple run) in unity ?(scripting help)

Recommended Posts

Sugavanas    212

How can i generate a running screen with different things in every level like in temple run. but i dont want it to be endless, every level there will be a end and the user can go to the next level ?

the code i have here is,

C#

     public Transform[] prefabs;

public int tiles;

public void GenerateLevel() {

for(int t = 0; t < tiles; t++) {

GameObject go = Instantiate(prefabs[Random.Range(0, prefabs.Length)]) as GameObject;

}

}


but all if the prefabs, model are being in the same place, how can i make it to go on it own position so it connects from one to another, and also when it rotates how can i make it to attach the next model after rotation.

Share on other sites
Kylotan    10008

You move objects in Unity by setting their transform's position. eg:

go.transform.position = new Vector3(1,2,3);


To attach one model to another you set the second object's parent to be the first object. Then you can set its position relative to that one.

It's best if you read through all the Unity scripting guide and reference - it's long, but most of what you need is in there. The stuff about transforms is here: http://docs.unity3d.com/Documentation/Components/class-Transform.html

Share on other sites
Sugavanas    212

You move objects in Unity by setting their transform's position. eg:

go.transform.position = new Vector3(1,2,3);


To attach one model to another you set the second object's parent to be the first object. Then you can set its position relative to that one.

It's best if you read through all the Unity scripting guide and reference - it's long, but most of what you need is in there. The stuff about transforms is here: http://docs.unity3d.com/Documentation/Components/class-Transform.html

ya i use the transform thing before, but i din't add the new vector3, i will try it and say about it

This is such a common use-case that Unity's Instantiate() method actually supports it directly (note the form of Instantiate() that takes 3 arguments).

thanks i will see it

Sugavanas    212

any others there

Kylotan    10008

Share on other sites
Sugavanas    212

my question i show can i generate a path like temple run but i don't want it to be endless, i want it to be end it some distance, or after some tiles, and at the end it will take the user to the next level, so how do i dgenerate the prefabs in the correct order without touching each other

Share on other sites
Kylotan    10008

Yes, we saw the original question, but we gave you the information on how to proceed. Instantiate each prefab at the position you want it to be in. To avoid them touching each other you need to know how wide they are, so you can position each one at least that far from the previous. Have you tried that?

Share on other sites
olgo    119

I recently created a similar script to generate random towers.  Each piece of the tower is 1 level.  The object's origin is 1 connecting point, I then place an empty GameObject in the prefab as another connecting point.  The script cycles through each prefab, finds the empty GameObject, and instantiates the next prefab within it.

The end result is a long list of parents with their children.  The base prefab of the tower is the upper most parent and each level up the tower is a child of the last piece.  You can have the script run in a for loop to determine how many modules you want to link end-to-end to control distance.

I think your original question asks (correct me if I'm wrong), how do I keep my path from making 3 right turns and colliding with itself?    Have a variable track how many right or left turns were made, don't instantiate a third right turn if 2 have already been made, force a left turn module to be placed.

Hope this helps.