This is what I tried :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateGrid : MonoBehaviour
{
public GameObject gridPrefab;
public GameObject pathPrefab;
private Transform[,] gridBlocks;
private List<Vector3> gridBlocksPositions = new List<Vector3>();
private List<Transform> gridOuterBlocks = new List<Transform>();
private List<Vector3> gridOuterBlocksX = new List<Vector3>();
private List<Vector3> gridOuterBlocksXLength = new List<Vector3>();
private List<Vector3> gridOuterBlocksZ = new List<Vector3>();
private List<Vector3> gridOuterBlocksZLength = new List<Vector3>();
private Vector3 startBlockPOs;
private Vector3 endBlockPos;
public int gridWidth = 10;
public int gridHeight = 10;
public int gap;
public float spawnSpeed = 0;
public bool gridGenerated = false;
void Start()
{
StartCoroutine(CreateGrid());
}
IEnumerator CreateGrid()
{
gridBlocks = new Transform[gridWidth, gridHeight];
for (int x = 0; x < gridWidth; x++)
{
yield return new WaitForSeconds(spawnSpeed);
for (int z = 0; z < gridHeight; z++)
{
yield return new WaitForSeconds(spawnSpeed);
GameObject block = Instantiate(gridPrefab, Vector3.zero, gridPrefab.transform.rotation) as GameObject;
block.transform.parent = transform;
block.transform.localPosition = new Vector3(x + x * gap, 0, z + z * gap);
block.tag = "Grid Block";
gridBlocks[x, z] = block.transform;
gridBlocksPositions.Add(block.transform.position);
if ((x == 0) || (x == gridWidth - 1) || (z == 0) || (z == gridHeight - 1))
{
if(x == 0)
{
gridOuterBlocksX.Add(block.transform.position);
}
if(x == gridWidth - 1)
{
gridOuterBlocksXLength.Add(block.transform.position);
}
if(z == 0)
{
gridOuterBlocksZ.Add(block.transform.position);
}
if(z == gridHeight - 1)
{
gridOuterBlocksZLength.Add(block.transform.position);
}
gridOuterBlocks.Add(block.transform);
var blockRenderer = block.GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.red);
}
}
}
PickRandom();
gridGenerated = true;
}
private void PickRandom()
{
Transform randBlock = gridOuterBlocks[Random.Range(0, gridOuterBlocks.Count)];
if(randBlock.position.x == 0)
{
startBlockPOs = randBlock.position;
}
if (randBlock.position.x == gridWidth - 1)
{
endBlockPos = gridOuterBlocksXLength[Random.Range(0, gridOuterBlocksXLength.Count)];
}
if (randBlock.position.z == 0)
{
startBlockPOs = randBlock.position;
}
if (randBlock.position.z == gridHeight - 1)
{
endBlockPos = gridOuterBlocksZLength[Random.Range(0, gridOuterBlocksZLength.Count)];
}
for(int i = 0; i < gridOuterBlocks.Count; i++)
{
if(startBlockPOs == gridOuterBlocks[i].position ||
endBlockPos == gridOuterBlocks[i].position)
{
var rend = gridOuterBlocks[i].GetComponent<Renderer>();
rend.material.SetColor("_Color", Color.blue);
}
}
}
}
But the result is all the time or 99% of the time that he pick the start position the 0,0 top left corner block and then the end position also at the top left corner the left example in the screenshot or he pick two positions start and end on same side the right example in the screenshot :
I can't make it to pick a random positions like I wanted in the two top screenshots examples with the light blue color.
The rules should be :
Never on the same side.
Never on the same position.
Always to pick the end position from the parallel wall side.