统一随机器生成结果为非随机梯度

问题描述 投票:0回答:1

我正在使用Sebastian Lague的 "制作游戏 "系列教程中的代码,到目前为止,一切都很容易适应新版本的unity。然而在 第9集我已经变得不稳定了。我已经修改了地图生成器的代码,使它能在我的unity版本中编译。2019.3.11f1. 然而,随机地图的生成似乎偏向于最低协调的地图。

我的Gen结果。

My Gen

它应该是什么样子的:

[target[2]

这段代码是地图生成器脚本,它被放置在世界中的一个空的地方,并添加立方体作为障碍,提供一些预制件。

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



public class MapGenerator : MonoBehaviour
{
    public Transform tilePrefab;
    public Vector2 mapSize;
    public int seed = 10;
    public Transform obstaclePrefab;
    public int obstacleCount = 10;



    [Range(0, 1)]
    public float outlinePercent;  // Cotrols scale of tiles 

    List<Coord> allTileCoords;
    Queue<Coord> shuffledTileCoords;

    private void Start()
    {
        GenerateMap();
    }


    public void GenerateMap()
    {

        allTileCoords = new List<Coord>();
        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                allTileCoords.Add(new Coord(x, y)); //iterate through mapSize adding tiles
            }
        }
        shuffledTileCoords = new Queue<Coord>(Utility.ShuffleArray(allTileCoords.ToArray(), seed)); //shuffled array of tiles coords

        string holderName = "Generated Map";                 //Added for the editor script
        if (GameObject.Find(holderName))                     //Added for the editor script
        {                                                    //Added for the editor script
            DestroyImmediate(GameObject.Find(holderName));   //Added for the editor script
        }                                                    //Added for the editor script

        Transform mapHolder = new GameObject(holderName).transform; //This is only neccessary because of the editor script
        mapHolder.parent = transform; //This is only neccessary because of the editor script

        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                Vector3 tilePosition = relativeToSpacial(x, y); //converts grid x,y data to real spatial coords
                Transform newTile = Instantiate(tilePrefab, tilePosition, Quaternion.Euler(Vector3.right * 90)) as Transform; //instantiates tile in loctaion from rel to spatial func with 90 rotation
                newTile.localScale = Vector3.one * (1 - outlinePercent); //scales down tiles to leave a outline
                newTile.parent = mapHolder;
            }
        }

        for (int i = 0; i < obstacleCount; i++)
        {
            Coord randomCoord = GetRandomCoord();
            Vector3 obstaclePosition = relativeToSpacial(randomCoord.x, randomCoord.y);
            Transform newObstacle = Instantiate(obstaclePrefab, obstaclePosition + Vector3.up * .5f, Quaternion.identity) as Transform;
            newObstacle.parent = mapHolder;
        }
    }

    Vector3 relativeToSpacial(int x, int y)
    {
        return new Vector3(-mapSize.x / 2 + .5f + x, 0, -mapSize.y / 2 + .5f + y);
    }


    public Coord GetRandomCoord()
    {
        Coord randomCoord = shuffledTileCoords.Dequeue();
        shuffledTileCoords.Enqueue(randomCoord);
        //print("|| " + randomCoord.x + " || " + randomCoord.y + " ||");
        return randomCoord;
    }

    public struct Coord
    {
        public int x;
        public int y;

        public Coord(int _x, int _y)
        {
            x = _x;
            y = _y;
        }
    }
}

这是来自 Utility.ShuffleArray 这是我的自定义函数,用于洗牌数组。

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


public static class Utility
{
    public static T[] ShuffleArray<T>(T[] array, int seed)
    {
        System.Random prng = new System.Random(seed);
        for (int i =0; i < array.Length -1; i++)
        {
            int randomIndex = prng.Next(i, array.Length);
            T tempItem = array[randomIndex];
            array[randomIndex] = array[i];
        }
        return array;
    }
}

感谢任何帮助。

c# unity3d instantiation
1个回答
2
投票
int randomIndex = prng.Next(i, array.Length);

你在你的for循环中的这一行增加了你的rng的夹子,所以它变得越来越偏向于数组的末尾,我还添加了代码来做一个交换的位置,而不是重复的位置,尝试像这样的东西

int randomIndex = prng.Next(0, array.Length);
T tempItem = array[randomIndex];
T tempItemTwo = array[i];
array[randomIndex] = array[i];
array[i] = tempItemTWo;

如果不工作也可以试试。

int randomIndex = prng.Next(i, array.Length);
T tempItem = array[randomIndex];
T tempItemTwo = array[i];
array[randomIndex] = array[i];
array[i] = tempItemTWo;
© www.soinside.com 2019 - 2024. All rights reserved.