# 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

but this is a very limited type.

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)
if(maplevel == 2)
if(maplevel == 3)
then something to actually trigger the loading the level
onTriggerEnter// or whatever
``````

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  *= 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*[i2]!=1){_
_
g=(GameObject)Instantiate(flattile,v,new Quaternion(0,0,0,0));_
_
}*_

_ if(squares*[i2]==1){
g=(GameObject)Instantiate(holetile,v,new Quaternion(0,0,0,0));*_

* }*

_ if(squares*[i2]==5){
g=(GameObject)Instantiate(someobject,new Vector3(v.x,1f,v.z),new Quaternion(0,0,0,0));*_

* }*

* }}*

}

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.