Skip to main content
added 5088 characters in body
Source Link
Daniel Lip
  • 1.8k
  • 4
  • 36
  • 79

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 :

Wrong results

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 :

  1. Never on the same side.

  2. Never on the same position.

  3. Always to pick the end position from the parallel wall side.

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 :

Wrong results

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 :

  1. Never on the same side.

  2. Never on the same position.

  3. Always to pick the end position from the parallel wall side.

Source Link
Daniel Lip
  • 1.8k
  • 4
  • 36
  • 79

How can I select random two positions from the grid outer square and also that the randoms will be from two walls facing each other?

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))
                {
                    gridOuterBlocks.Add(block.transform.position);

                    var cubeRenderer = block.GetComponent<Renderer>();
                    cubeRenderer.material.SetColor("_Color", Color.red);
                }
            }
        }

        gridGenerated = true;
    }

Here I color the outer grid blocks in red. But now I want to color in blue two positions each time and each one from the other outer side.

So each time I'm running the game it should pick two random positions each one from the parallel sides : For example :

Random pick

Or like this :

Random pick

So the two random positions will be picked from two parallel sides.