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 Kenshin_ · Aug 15, 2014 at 08:39 AM · endless runnerobstacle

How can I make Random Obstacle Spawn randomize between 4 or more prefabs?

Hello I am new to Unity and I've been trying to make a vertical endless runner in which I'm trying to randomize obstacles, I have already set up for one prefab, but I'm trying to make it randomly choose between 4 prefabs. Here's what I have so far.

 enter code here   public Transform prefab;
     public int numberOfObjects;
     public float recycleOffset;
     public Vector3 startPosition;
     public Vector3 minSize, maxSize, minGap, maxGap;
     public float minY, maxY;
  
     private Vector3 nextPosition;
     private Queue<Transform> objectQueue;
  
     void Start () {
         objectQueue = new Queue<Transform>(numberOfObjects);
         for(int i = 0; i < numberOfObjects; i++){
             objectQueue.Enqueue((Transform)Instantiate(prefab));
         }
         nextPosition = startPosition;
         for(int i = 0; i < numberOfObjects; i++){
             Recycle();
         }
     }
  
     void Update () {
         if(objectQueue.Peek().localPosition.y + recycleOffset < Player.distanceTraveled){
             Recycle();
         }
     }
  
     private void Recycle () {
         Vector3 scale = new Vector3(
             Random.Range(minSize.x, maxSize.x),
             Random.Range(minSize.y, maxSize.y),
             Random.Range(minSize.z, maxSize.z));
  
         Vector3 position = nextPosition;
         position.x += scale.x * 0.5f;
         position.y += scale.y * 0.5f;
  
         Transform o = objectQueue.Dequeue();
         o.localScale = scale;
         o.localPosition = position;
         objectQueue.Enqueue(o);
  
         nextPosition += new Vector3(
             Random.Range(minGap.x, maxGap.x),
             Random.Range(minGap.y, maxGap.y)+ scale.y,
             Random.Range(minGap.z, maxGap.z));
  
         if(nextPosition.y < minY){
             nextPosition.y = minY + maxGap.y;
         }
         else if(nextPosition.y > maxY){
             nextPosition.y = maxY - maxGap.y;
         }
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Foose · Aug 15, 2014 at 11:55 AM

I always use this one. It lets you handle how much prefabs you wanna take, choose a random prefab, vary the spawntime and takes gameobjects as spawnpoints.

 public class mobGenerator : MonoBehaviour {
     public enum State {
         Idle,
         Initialize,
         Setup,
         SpawnMob
     }
     public GameObject[] mobPrefabs; //an array to hold all of the prefabs of mobs we want to spawn
     public GameObject[] spawnPoints; // this array will hold a reference to akk the spawn points in the scene
 
     public State state;     //this is our local variable that holds our current state
     public int mobsNumber = 1;
     public float countSpawnTimer;
     public float spawnTimer = 10;
     GameObject child;
 
     void Awake () {
         state = mobGenerator.State.Initialize;
         countSpawnTimer = spawnTimer;
 
         }
 
 
     // Use this for initialization
     IEnumerator Start () {
         while (true) {
             switch(state) {
             case State.Initialize:
                 Initialize();
                 break;
             case State.Setup:
                 Setup();
                 break;
             case State.SpawnMob:
                 SpawnMob();
                 break;
             }
 
             yield return 0;
                 }
     
     }
 
     void Update() {
         countSpawnTimer -= Time.deltaTime;
         if (countSpawnTimer <= 0) {
             countSpawnTimer = 0;
             state = mobGenerator.State.Initialize;
             }    
     }
 
     private void Initialize() {
         countSpawnTimer = spawnTimer;
         if (!CheckForMobPrefabs ())
                 return;
         if (!CheckForSpawnPoints ())
                 return;
         state = mobGenerator.State.Setup;
         }
 
     private void Setup() {
         state = mobGenerator.State.SpawnMob;
     }
     private void SpawnMob() {
         GameObject[] gos = AvailableSpawnPoints ();
 
         for (int cnt = 0; cnt < mobsNumber; cnt++) {
             GameObject go = Instantiate(mobPrefabs[Random.Range(0, mobPrefabs.Length)],
                                         gos[Random.Range (0, gos.Length)].transform.position,
                                         Quaternion.identity
                                         ) as GameObject;
             go.transform.parent = gos[cnt].transform;
             Destroy(go, spawnTimer);
                 }
         state = mobGenerator.State.Idle;
     }
 
     //check to see that we have at least one mob prefab to spawn
     private bool CheckForMobPrefabs() {
         if (mobPrefabs.Length > 0)
                 return true;
         else
                 return false;
         }
 
     //check to see that we have at least one spawnpoint to spawn mobs
     private bool CheckForSpawnPoints() {
         if (spawnPoints.Length > 0)
             return true;
         else
             return false;
         }
 
     //generate a list of available spawn points that do not have any mobs child to it
     private GameObject[] AvailableSpawnPoints() {
         List<GameObject> gos = new List<GameObject> ();
 
         for(int cnt = 0; cnt < spawnPoints.Length; cnt++) {
             if(spawnPoints[cnt].transform.childCount == 0) {
                 gos.Add(spawnPoints[cnt]);
             }
         }
         return gos.ToArray ();
         }
 
 }

Comment
Add comment · Show 2 · 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 Kenshin_ · Aug 15, 2014 at 08:21 PM 0
Share

Awesome. I just implemented the array you have onto my code and its working pretty well Thanks

avatar image skyyguy · Jun 22, 2016 at 05:03 AM 0
Share

I am having a similar problem and tried to implement your script above but had issues - would you look at http://stackoverflow.com/questions/37958644/unity-c-trying-to-randomly-choose-from-prefabs-in-queue-to-recycle-error-wit

avatar image
0

Answer by zaid87 · Aug 15, 2014 at 09:16 AM

A simple way that I can think of is

  1. Make an array of prefabs

  2. Randomise a number between 1-4

  3. Use the number to select which prefab to use

Comment
Add comment · Show 1 · 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 Kenshin_ · Aug 15, 2014 at 05:49 PM 0
Share

How do you implement that into the code that I have posted? I haven't really worked with arrays, so I'm a bit confused on how to go about adding that.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Random Obstacle Placement 0 Answers

random obstacle generator 2 Answers

How do I increase the speed an instantiated object spawns in with? 0 Answers

Spawn Different Obstacles for certain platforms 1 Answer

UnitySteer obstacle avoidance on non Spherical Objects 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