Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Sugavanas   Members   -  Reputation: 169

Like
0Likes
Like

Posted 18 February 2013 - 06:09 AM

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.



Sponsor:

#2 Kylotan   Moderators   -  Reputation: 3338

Like
0Likes
Like

Posted 18 February 2013 - 01:01 PM

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



#3 swiftcoder   Senior Moderators   -  Reputation: 9846

Like
1Likes
Like

Posted 18 February 2013 - 03:49 PM

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).


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#4 Sugavanas   Members   -  Reputation: 169

Like
0Likes
Like

Posted 19 February 2013 - 02:08 AM

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



#5 Sugavanas   Members   -  Reputation: 169

Like
0Likes
Like

Posted 20 February 2013 - 01:53 AM

any others there



#6 Kylotan   Moderators   -  Reputation: 3338

Like
0Likes
Like

Posted 20 February 2013 - 05:59 AM

What's your question?



#7 Sugavanas   Members   -  Reputation: 169

Like
0Likes
Like

Posted 21 February 2013 - 01:49 AM

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



#8 Kylotan   Moderators   -  Reputation: 3338

Like
1Likes
Like

Posted 21 February 2013 - 06:42 AM

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?



#9 olgo   Members   -  Reputation: 118

Like
0Likes
Like

Posted 21 February 2013 - 02:27 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS