- Home /
Create passages in round maze
I've tried to find the answer by myself, but didn't succeeded so far.
I've making round maze. After instantiating every circle of walls ( for every circle - copy of the same script, because i need different number of wall parts in every circle ), the part of the wall has 10% chance to be destroyed - it creates passages by now. The problem is that it's not a good solution, i need not more than 3 passages in every circle of walls. But with random it can be more than 3, or even zero.
So how to limit the number of passages in instantiated circles of walls? Maybe it's better to make circle of walls by hand, without instantiating them and give every part of the wall unique name, and then make if/else with random? For example, 10% that Wall 1 will be destroyed, and if not, 10% that Wall 2 will be destroyed. And when 3 parts will be destroyed, end the chain. But isn't it a hard way?
It is hard to be specific without your source code. I assume you know how many wall segments go into each wall. Just pick three (or however many) before you generate each circle, and don't generate a wall segment if the segment number is one of the three.
Wall Generation:
public class Walls1 : $$anonymous$$onoBehaviour { public GameObject prefab; public int numberOfObjects = 20; public float radius = 5f; void Start () { for (int i = 0; i < numberOfObjects; i++) { float angle = i $$anonymous$$athf.PI 2 / numberOfObjects; Vector3 pos = new Vector3 ($$anonymous$$athf.Cos (angle), 0, $$anonymous$$athf.Sin (angle)) * radius; GameObject Wally = Instantiate (prefab, pos, Quaternion.LookRotation(-pos)) as GameObject; Wally.transform.parent = transform; Wally.AddComponent(); } } Each part of the wall receive script: > Blockquote void Start () { var theRange = Random.Range(0,12); if (theRange == 1) { Destroy (gameObject); } } }Blockquote
I'd make your choices before you go into the loop and create the objects. get your three random numbers based on "numberOfObjects" and then have an if inside your for loop saying something like this.
for (int i = 0; i < numberOfObjects; i++) {
if (luckynumbers[0]==i||luckynumbers[1]==i||luckynumbers[2]==i){
//Don't build it
}else{
//build it
}
}
Answer by robertbu · Aug 04, 2014 at 03:47 PM
I cannot think of a slick way to do this. Here is a brute force way. It generates a generic list of all the possible segments, and then eliminates all but a specified number. At the top of the file put:
using System.Collections.Generic;
Then Add this function:
List<int> FindHoles(int totalSegments, int holeCount) {
List<int> holes = new List<int>();
for (int i = 0; i < totalSegments; i++) {
holes.Add(i);
}
while (holes.Count > holeCount) {
holes.RemoveAt (Random.Range (0,holes.Count));
}
return holes;
}
Then your generation code would be:
void Start () {
List<int> holes = FindHoles(numberOfObjects, 3);
for (int i = 0; i < numberOfObjects; i++) {
float angle = i * Mathf.PI * 2 / numberOfObjects;
Vector3 pos = new Vector3 (Mathf.Cos (angle), 0, Mathf.Sin (angle)) * radius;
if (!holes.Contains(i)) {
GameObject Wally = Instantiate (prefab, pos, Quaternion.LookRotation(-pos)) as GameObject;
Wally.transform.parent = transform;
Wally.AddComponent<D>();
}
}
}
Note I've specified 3 for the holeCount. This value could be randomly generated, or it could be a fraction of the total number of segments. Also with this code, you don't need the scrip on each segment, since hole objects are never created.
http://answers.unity3d.com/questions/981285/difficult-question.html
pls help robertbu :P