0

I'm attempting to make a 3D Endless Runner-type game. The pathway is 5 blocks wide and 1-4 of the 5 blocks need to generate every 50 units on the Z axis so the player has a way to proceed.

So far, I've been making each combination (31 of them) get chosen from an existing prefab, but making each combination separately is a bit of a pain. Not to mention it takes up more memory then id like it to.

Is there a way to have the blocks generate randomly across the X-Axis every 50 units along the Z-Axis?

Here's the code I've been using so far. (Just figured out how to post it)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateLevel : MonoBehaviour
{
    public GameObject[]  section;
    public int zPos = 50;
    public bool creatingSection = false;
    public int secNum;
    
    void Update()
    {
        if (creatingSection == false)
        {
            creatingSection = true;
            StartCoroutine(GenerateSection());
        }
    }

    IEnumerator GenerateSection()
    {
        secNum = Random.Range(0, 31);
        Instantiate(section[secNum], new Vector3(0, 0, zPos), Quaternion.identity);
        zPos += 50;
        yield return new WaitForSeconds(2);
        creatingSection = false;
    }
}
Geeky Quentin
  • 2,469
  • 2
  • 7
  • 28
Pete
  • 1
  • 2
  • welcome to stack overflow :). What have you tried that is not working? – rustyBucketBay Jul 27 '22 at 04:56
  • Thank you! I havent done any C++ in about 9 years so re learning everything has been rough. So far I've tried creating the different combinations as sections then having unity pick one from random and generate them. I can get that to work but it uses a bunch of resources as i have to have the existing sections off screen to be copied – Pete Jul 27 '22 at 05:02
  • post the code so that people can have a look and maybe help with what is not working. You can expect the community to help out, but not to provide the solution for you from scratch :) – rustyBucketBay Jul 27 '22 at 05:11
  • I'm having some trouble getting this to identify the code to post it. – Pete Jul 27 '22 at 05:31
  • I see you already instantiate an random elemente from an array, so I don't understand your issue as that seems to me exactly what you are asking for. – bolov Jul 27 '22 at 06:48

1 Answers1

0

I think I get what you're trying to do. So create a single Prefab for a single block and then create unique permutations of a set (like in this answer: here)

public GameObject blockPrefab;
private List<float> possibleXPositions = {10f, 20f, 30f, 40f, 50f};

[...]

IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> items, int count)
{
    int i = 0;
    foreach(var item in items)
    {
        if(count == 1)
            yield return new T[] { item };
        else
        {
            foreach(var result in GetPermutations(items.Skip(i + 1), count - 1))
                yield return new T[] { item }.Concat(result);
        }

        ++i;
    }
}

And then use it to generate the permutation you want:

List<List<float>> positions = GetPermutations(possibleXPositions, Random.Range(1,5));
List<float> chosenPositions = positions[Random.Range(0, positions.Count)];
foreach(float x in chosenPositions){
    Instantiate(section[secNum], new Vector3(x, 0, zPos), Quaternion.identity);
}
Shrimp
  • 514
  • 5
  • 9