Jump to content
  • Advertisement
Sign in to follow this  
Sugavanas

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

This topic is 2004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 this post


Link to post
Share on other sites

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 this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!