Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tacticz_03 · Jan 20, 2015 at 03:35 PM · randomspawngenerate

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.

alt text

here is the video Game video

drive.jpg (58.4 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image richyrich · Jan 20, 2015 at 03:43 PM 1
Share

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

avatar image meat5000 ♦ · Jan 23, 2015 at 02:57 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tacticz_03 · Jan 22, 2015 at 10:59 AM 0
Share

Hi richyrich. I found some codes related to my problem. Can you help with this?

Here's the link. generate item

avatar image richyrich · Jan 22, 2015 at 12:35 PM 0
Share

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

avatar image tacticz_03 · Jan 23, 2015 at 02:54 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do you make a spawner block? 1 Answer

Spawn a gameobject in random position within an area? 2 Answers

Spawn random amount apart of each other 1 Answer

How to make levels spawn randomly? 0 Answers

Spawning at a random position away from the player 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges