- Home /
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.
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
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.
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);
Hi thanks for your answer but this code will only load level randomly want i want is to create level randomly.
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));
}
}}
}
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.
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!
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.
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.
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
Follow this Question
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