- Home /
Spawning issues
I have a project that is essentially just an endless runner. I just recently have coded in the part that makes it so I cannot have two blocks spawn adjacent to each other that will block the path to the player.
I do so by creating prefabs for all 19 possible platforms: (0 denotes clear, x denotes pitfall and b denotes block)
platform1: 000
platform 2: b00
platform 3: bx0
platform 4: bb0
platform 5: b0x
platform 6: b0b
platform 7: x00
platform 8: xb0
platform 9: xx0
platform 10: x0b
platform 11:x0x
platform 12: 0b0
platform 13: 0bb
platform 14: 0bx
platform 15: 0x0
platform 16: 0xb
platfrom 17: 0xx
platform 18: 00b
platform 19: 00x
Using this key I created 7 different "classes" that blocks fall into and which blocks cannot be adjacent to them.
all clear:
contains: 1
cannot adjacent: none
left covered:
contains: 2, 7
cannot adjacent: 13, 14, 16, 17
left & mid covered:
contains: 3, 4, 8, 9
cannot adjacent: 5, 6, 10, 11, 13, 14, 16, 17, 18, 19
left & right covered:
contains: 5, 6, 10, 11
cannot adjacent: 3, 4, 8, 9, 12, 13, 14, 15, 16, 17
middle covered:
contains: 12, 15
cannot adjacent: 5, 6, 10, 11
middle & right covered:
contains: 13, 14, 16, 17
cannot adjacent: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
right covered:
contains: 18, 19
cannot adjacent: 3, 4, 8, 9
Using this information, I created 7 different GameObject[]'s that contain only blocks that are safe to the 7 areas. (i.e. in GameObject[] left_safe_walkways I have all platforms except for 13, 14, 16, and 17).
With all of this in mind, I created an IENumerator that currently spawns up to 100 platforms using a for loop. I have, since using code to limit the block spawns, have still been seeing impossible parts in the game. Is there anything I can do to fix it? Here's the code I am using currently: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class spawnwalkway : MonoBehaviour {
public GameObject[] all_walkways;
public GameObject[] left_safe_walkways;
public GameObject[] left_mid_safe_walkways;
public GameObject[] left_right_safe_walkways;
public GameObject[] mid_safe_walkways;
public GameObject[] mid_right_safe_walkways;
public GameObject[] right_safe_walkways;
public int walkway_number;
public int next_walkway_number;
int plat_num_random;
// Use this for initialization
void Start () {
StartCoroutine(spawnBlock());
}
// Update is called once per frame
void Update () {
}
IEnumerator spawnBlock()
{
int walkway_number = Random.Range (0, 18);
for (int z = 1; z < 100; z++)
{
if (walkway_number == 0) {
next_walkway_number = Random.Range (0, 18);
Debug.Log (all_walkways [next_walkway_number] + "block "+ z);
Instantiate (all_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
} else
if (walkway_number == 1 || walkway_number == 6) {
next_walkway_number = Random.Range (0, 14);
Debug.Log (left_safe_walkways [next_walkway_number] + "block "+ z);
Instantiate (left_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
} else
if (walkway_number == 2 || walkway_number == 3 || walkway_number == 7 || walkway_number == 8) {
next_walkway_number = Random.Range (0, 8);
Debug.Log(left_mid_safe_walkways[next_walkway_number] + "block "+ z);
Instantiate (left_mid_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
} else
if (walkway_number == 4 || walkway_number == 5 || walkway_number == 9 || walkway_number == 10) {
next_walkway_number = Random.Range (0, 8);
Debug.Log (left_right_safe_walkways [next_walkway_number] + "block "+ z);
Instantiate (left_right_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
} else
if (walkway_number == 11 || walkway_number == 14) {
next_walkway_number = Random.Range (0, 14);
Debug.Log(mid_safe_walkways[next_walkway_number] + "block "+ z);
Instantiate (mid_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
} else
if (walkway_number == 12 || walkway_number == 13 || walkway_number == 15 || walkway_number == 16) {
next_walkway_number = Random.Range (0, 8);
Debug.Log(mid_right_safe_walkways[next_walkway_number] + "block "+ z);
Instantiate (mid_right_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
} else
if (walkway_number == 17 || walkway_number == 18) {
next_walkway_number = Random.Range (0, 14);
Debug.Log(right_safe_walkways[next_walkway_number] + "block "+ z);
Instantiate (right_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
}
walkway_number = next_walkway_number;
yield return new WaitForSeconds(0.5f);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612161812im_/https://answers.unity.com/themes/thub/images/avi.jpg)