- Home /
How to generate random pickup items on EasyRoads3D?
Hi! I am developing a game looks like temple run and subway surfers but my game is not endless. I'm using EasyRoads3D for the road and I want to generate pickup items in the road. I've searched related topics but I didn't find one.
here is my game screenshot where I placed the items manually.
here is the video Game video
@unimechanic Presumably it is you that converted my comment to an answer. However, there seems to be a bug... if you convert a comment to an answer in the questions list, then publish the post, the answer disappears. It has happened when I do it in the past, but also by official staff too it would now seem.
This happens if you select any option other than "Viewable by all users". I have documented it in The Bug Thread and dZone should be aware of the problem.
Answer by richyrich · Jan 20, 2015 at 04:01 PM
You need to use a random function to identify the kind of object to place. Lets say you have 10 types. So you would use code from here: http://docs.unity3d.com/ScriptReference/Random.Range.html
Example
//10 is exclusive so you can get any value between 0-9 inclusive
int arrayIndexForNextFruit = Random.Range(0, 10);
Using a switch statement, you could then identify the object to be used
You could implement the above in different ways. One approach uses 3 arrays
1) Array of different kinds of objects you can have
2) Array of positions for the objects
3) Array of GameObjects that get placed
You could then setup the third array by assigning values identical to objects of the first array (index via random method). You array index of the third array element being set, would be the same index for obtaining position from second array
This avoids instantiating all the time, increasing speed and reduce garbage
Hi richyrich. I found some codes related to my problem. Can you help with this?
Here's the link. generate item
The principle answer by aldonaletto instantiates prefabs in random locations roughly similar to the pseudo code I have provided in my answer, although the types used differ and are an improvement on $$anonymous$$e.
In the Start function, the array of 'items' is similar to my third array i.e. a collection that gets placed.
Within the for loop, objects are created from prefabs by being instantiated (created) using the Instantiate function which selects a prefab template, identified via the index defined by the random function I mentioned in my answer (uses the int version)
A move function is then called which again uses the Random, but the float range function ins$$anonymous$$d of int. This works out an X value for the position (XYZ), you can leave that as is assu$$anonymous$$g your path runs largely from -X to +X in your world. The Y and Z values could be defined by constants (this makes sense if say you wanted all objects to appear in the centre of the road and assumes your Y value is constant too - which it would be if your terrain was flat.
That is essentially it. There is a lot of use of angles associated with a spherical world, much of which you won't need.
A word of caution though, the term 'procedurally generated' can refer to the use of objects created through code rather than from a prefab' ...It is highly mathematical in nature and will add significantly to the complexity of your project. However, the contextual meaning in this case is runtime placement of prefabs which is considerably easier to use.
If you need further ideas, the key thing to look for is how to use the Instantiate function. There's some info in the docs but you'll find more useful stuff (I$$anonymous$$O) in Unity Answers from people who have already had similar problems solved
Thank you for your quick response. I'll try what you've said. But I am a beginner, and its quite difficult because my terrain is not flat; it has slopes.