Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 umair-arshad · Apr 05, 2017 at 12:42 PM · randomlevelgeneration

how to create random levels. like a golf game

im creating a game. where you pocket a ball, there are different hurdles and a boundary. ball can be pocket by hitting a boundary wall or a hurdle.

what i want is a level generator which generator level randomly, place hurdles with in boundary and a pocket in such a way that it is possible to pocket the ball.

i follow this technique https://gamedevelopment.tutsplus.com/tutorials/bake-your-own-3d-dungeons-with-procedural-recipes--gamedev-14360

but this is a very limited type.

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 toddisarockstar · Apr 05, 2017 at 01:53 PM 0
Share

i would need some picture to understand what your level would look like.

are boundrys smooth or block like?

do you want boundarys roughly in the shape of fairways in golf?

off the top of my head here is completely different approach that might be cool.

you could use a texture and height map approach,

you could start by drawing a bunch of predefined shapes in paint.

then write a script to randomly copy/paste those shapes into a a new bigger Texture2D.

after getting a random looking Texture that looks like where your boundarys should be, you can really do about anything with it.

you could use it as a heightmap to raise walls or generate triggers for your boundarys.

if you made the texture size match your world size. you could use different colors to represent areas that are O$$anonymous$$ areas to randomly place particular objects.
a different color channel could represent an O$$anonymous$$ place to randome

avatar image umair-arshad toddisarockstar · Apr 06, 2017 at 05:19 AM 0
Share

alt text this is the game idea and i want to create these levels randomly. Randomly place holes and hurdles in such a way that it is possible to pocket ball.

f.png (142.6 kB)

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by shadowpuppet · Apr 05, 2017 at 07:01 PM

probably not the best way to do it but this is what I did to generate random object - same principle but will try to adapt for levels

   public int  mapLevel;//leave blank;
     public string levelToLoad;//leave blank;
  public string level1;
  public string level2;
  public string level3;//this is where you put the names of the possible levels to be loaded
     void Awake();
     mapLevel = Random.Range (1,4);//this randomly generates either a 1,2 or a 3
     then assign a level to that number
     void start();
     if(maplevel == 1)
     levelToLoad = level1
     if(maplevel == 2)
     levelToLoad = level2
     if(maplevel == 3)
     levelToLoad = level3
     then something to actually trigger the loading the level
     onTriggerEnter// or whatever
     Application.LoadLevel(levelToLoad);



  
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 umair-arshad · Apr 06, 2017 at 04:36 AM 0
Share

Hi thanks for your answer but this code will only load level randomly want i want is to create level randomly.

avatar image
0

Answer by toddisarockstar · Apr 08, 2017 at 06:01 PM

Hmmm i would make the floor consist of gameobject tiles. draw one normal tile and one with a hole in it and spawn between the two of them randomly to create the floor. Your square or cubes that make the floor would need to be the size of exactly 1 wide and 1 long to appear seamless.

this script also places an object over the floor randomly avoiding the holes of course!

here is a script:

     int i;
     int i2;
     int ix;
     int iy;
     bool b;
 
     float f;
     Vector3 v;
 
     GameObject g;
 
     public int levelwidth;
     public int levellength;
 
 
     //drop your floor square with a hole into the inspector here
     public GameObject holetile;
 
     //drop your floor square without a hole into the inspector here
     public GameObject flattile;
     public int numberofholes;
 
     //drop an object to be placed on top here
     public GameObject someobject;
     public int numberofobjects;
 
     //make a two dimentional variable to make things easy
 
 
 
     public int[][] squares;
 
     // Use this for initialization
     void Start () {
                 levelwidth = 20;
                 levellength = 30;
 
                 numberofholes = UnityEngine.Random.Range (5, 10);
                 numberofobjects = UnityEngine.Random.Range (5, 10);
 
                 //initialize our 2d array
                 squares = new int[levelwidth][];
                 i = squares.Length;
                 while (i>0) {i--;
                         squares [i] = new int[levellength];
                 }
 
         i = numberofholes;
         //generate random locations for holes and mark them on our variable as 1;
         while (i>0) {i--;
                 b=false;
                while(!b){
 
                 //the second loop prevents double positions
                 ix=UnityEngine.Random.Range(0,levelwidth);
                 iy=UnityEngine.Random.Range(0,levellength);
 
                 if(squares[ix][iy]==0){b=true;
                     squares[ix][iy]=1;
                     print ("hole tile at: "+ix+" "+iy);
                 }
 
                 }
 
         }
 
         i = numberofobjects;
         //do the same for obsticle and mark them on our variable as 5;
         while (i>0) {i--;
             b=false;
             while(!b){
                 ix=UnityEngine.Random.Range(0,levelwidth);
                 iy=UnityEngine.Random.Range(0,levellength);
                 
                 if(squares[ix][iy]==0){b=true;squares[ix][iy]=5;
                     print ("obsticle at: "+ix+" "+iy);
                 }
                 
             }
             
         }
 
         // now that we have info on what our level should look like lets spawn it
 
 
         i = levelwidth;
 
         while(i>0){i--;
             i2 = levellength;
             while(i2>0){i2--;
                 v=new Vector3((float)i,0f,(float)i2);
 
                 if(squares[i][i2]!=1){
                     g=(GameObject)Instantiate(flattile,v,new Quaternion(0,0,0,0));
                     }
 
                 if(squares[i][i2]==1){
                     g=(GameObject)Instantiate(holetile,v,new Quaternion(0,0,0,0));
                 
                 }
 
                 if(squares[i][i2]==5){
                     g=(GameObject)Instantiate(someobject,new Vector3(v.x,1f,v.z),new Quaternion(0,0,0,0));
 
                 }
 
             }}
 
 }
 

 
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 umair-arshad · Apr 10, 2017 at 06:51 AM 0
Share

Hi toddisarockstar The script you provide is good. it generates floor , holes and hurdles randomly but the thing is how i authenticate it that it is possible to pocket ball. it just generate hurdles randomly.

avatar image toddisarockstar umair-arshad · Apr 21, 2017 at 03:29 AM 0
Share

this script does also generates floor holes randomly. This gives you the option of game object squares for the floor. they are gameobjects so you would simply put a hole in the hole tile prefab. If you needed anything beyond unitys physics like triggers or script to make the ball interact with a whole you would attach that to the floortile game object too! but thats another question and without this answer / theory marked as exceptable there is no purpose of me posting further!

avatar image
0

Answer by SohailBukhari · Apr 10, 2017 at 08:24 AM

You can do this as :

  • Draw a Grid

  • Instantiate your holes(pocket).

  • Save the Positions of pockets on the grid.

  • Now, instantiate the hurdles(exclude the pockets position)

  • Instantiate you Ball(Player).

  • Draw line from player towards the hurdle and Reverse Transform towards hole(pocket).

  • If Line Touches The hole then place next hurdle

  • If line not touches the hurdle then change the Positions of hurdle and redraw line.

Reference Image


random-level-min.png (458.1 kB)
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 umair-arshad · Apr 10, 2017 at 09:33 AM 0
Share

thanks sohail for your answer. but this is not what im looking. ball can pocket directly without hitting hurdle.sorry but your answer is too general.

avatar image SohailBukhari umair-arshad · Apr 10, 2017 at 09:51 AM 0
Share

Yes when you place hurdle then after hitting hurdle can also pot and directly you can also pot,you can see in the picture there is a possibility of potting direct without using hurdle, if this is not the case then explain your problem? If you don't want to pot by using hurdle then randomly place anywhere.

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

6 People are following this question.

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

Related Questions

Procedural level generation for a 3D side-scroller 2 Answers

Strange random seed issue with level generation 1 Answer

Randomly Generated Levels 3 Answers

Help with Generating Random Tiles 1 Answer

Random Button / Plane Generation. 0 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