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 aman_jha · Sep 01, 2014 at 10:01 PM · c#androidspawncube

Procedural Spawning Theorems

Hey guys, simple task here that I would like a bit of fine-tuning.

So basically I'm trying to re-make this game called Cube Runner and publish it for Android because I can't find any good versions of the game and I thought I'd make my own ;).

The link is at http://cuberunnergame.com/

I have mostly the entire game set up, but I have a pretty crappy cube spawn system. I use the following code to spawn the cubes:

 void SpawnObstacles () {
         int r = Random.Range (0, Obstacles.Length);
         GameObject obstacle1 = Instantiate (Obstacles[r], new Vector3((Random.Range (transform.position.x - 150f, transform.position.x + 150f)), 5f, transform.position.z + 300f), Quaternion.identity) as GameObject;
         obstacle1.name = Obstacles[r].name;
         obstacle1.transform.localScale *= Random.Range (.8f, 1.2f);
 
         int m = Random.Range (0, Obstacles.Length);
         GameObject obstacle2 = Instantiate (Obstacles[m], new Vector3((Random.Range (transform.position.x - 150f, transform.position.x + 150f)), 5f, transform.position.z + 300f), Quaternion.identity) as GameObject;
         obstacle2.name = Obstacles[m].name;
         obstacle2.transform.localScale *= Random.Range (.8f, 1.2f);
     
         int v = Random.Range (0, Obstacles.Length);
         GameObject obstacle3 = Instantiate (Obstacles[v], new Vector3((Random.Range (transform.position.x - 150f, transform.position.x + 150f)), 5f, transform.position.z + 300f), Quaternion.identity) as GameObject;
         obstacle3.name = Obstacles[v].name;
         obstacle3.transform.localScale *= Random.Range (.8f, 1.2f);
 
         int b = Random.Range (0, Obstacles.Length);
         GameObject obstacle4 = Instantiate (Obstacles[b], new Vector3((Random.Range (transform.position.x - 150f, transform.position.x + 150f)), 5f, transform.position.z + 300f), Quaternion.identity) as GameObject;
         obstacle4.name = Obstacles[b].name;
         obstacle4.transform.localScale *= Random.Range (.8f, 1.2f);
     }


Basically, I have an array of obstacles that are spawned randomly and with random sizes in random positions in front of the player. The problem is that some of the cubes spawn on top of each other, some of them are also sometimes too close together, and many times I get an impossible set-up. In the original cube runner, the cubes are spawned slightly spread out and the game is almost never completely impossible.

Does anyone have a better algorithm for spawning cubes? Or an idea on how I would implement it?

Thanks in advance, Aman Jha

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 AlucardJay · Sep 01, 2014 at 10:14 PM 0
Share

All this could be done in a for loop.

 void SpawnObstacles () 
 {
     int r;
     float posX;
     float scale;
     GameObject obstacle;
     
     for ( int i = 0; i < 4; i ++ )
     {
         r = Random.Range (0, Obstacles.Length);
         posX = Random.Range (transform.position.x - 150f, transform.position.x + 150f);
         scale = Random.Range (.8f, 1.2f);
         obstacle = Instantiate (Obstacles[r], new Vector3(posX, 5f, transform.position.z + 300f), Quaternion.identity) as GameObject;
         obstacle.name = Obstacles[r].name;
         obstacle.transform.localScale *= scale;
     }
 }

edit : there is a thread in the forums that you may find interesting : http://forum.unity3d.com/threads/how-to-randomly-instantiate-cubes-that-dont-overlap.263161/#post-1755624

avatar image aman_jha · Sep 02, 2014 at 02:40 AM 0
Share

Thanks for the for loop idea *facepalm. I was speed coding this app, everything was to be done in a day and I was kinda rushing. Thanks for the link too, I'll check it out!!

0 Replies

· Add your reply
  • Sort: 

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn object problem 1 Answer

screenshot 0 Answers

How To Call Health Script From Another Script ? 1 Answer

Android Development; How to use SDK manager! 1 Answer

Unity Scale One Text Field GUI 2 Answers


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