【问题标题】:Spawning random gameObjects into set positions将随机游戏对象生成到设定位置
【发布时间】:2023-04-05 07:51:01
【问题描述】:

我是编码新手,答案可能很简单。 我正在统一制作一个配对记忆游戏,我希望每次玩家加载游戏时卡片随机产生。我有一个卡片的游戏对象列表和一个我希望卡片生成的生成位置的变换列表。 到目前为止,我已经设法将卡片随机洗牌。但要做到这一点,我已经将纸牌游戏对象列表转换为整数。

public List<GameObject> cardsToSpawn;
public List<Transform> cardPositions = new List<Transform>();
public int cardCount;
public int someNum;
public List<GameObject> cards;
public GameObject empty;

void Awake()
{


    // list of the card gameobjects.
    List<int> cardsToSpawn = new List<int>();

        cardsToSpawn.Add(1);
        cardsToSpawn.Add(2);
        cardsToSpawn.Add(3);
        cardsToSpawn.Add(4);

        //Randomises the order. A, B, C, D = C, D, B, A for example.
        //cardsToSpawn is assigned a random number (1-18(length of cardsToSpawn list)).

    for (int i = 0; i < cardsToSpawn.Count; i++) 
      {
        int temp = cardsToSpawn[i];
        int randomIndex = Random.Range(i, cardsToSpawn.Count);
        cardsToSpawn[i] = cardsToSpawn[randomIndex];
        cardsToSpawn[randomIndex] = temp;

        //spawn card i=1 in cardPositions[1], card i=2 in cardPositions[2]... 
        temp = cards
        Instantiate(cardsToSpawn[i], cardPositions[i].position, transform.rotation);
        // but this refers back to the original list declared at the beginning of the script rather than the new randomIndex int.
      }
    // randomises the order. A, B, C, D = C, D, B, A for example.
    //tell me the order (i.e. make sure it is randomised.)
    cardsToSpawn.ForEach(i => Debug.Log("{0}\t"+ i));   


}

【问题讨论】:

标签: c# unity3d


【解决方案1】:

在您的for 循环中,您正在生成一个新的Random.Range ...这并不排除重复,另一方面也不保证所有卡片都已放置。

您更愿意做的是随机化 一个 列表(当然,您可以同时随机化这两个列表,但这实际上不会获得更多随机性),然后遍历所有元素以放置它们。

对于洗牌有多种选择。您可以使用extension method 之类的this one 或(因为我现在更容易解释)使用Linq OrderBy,如here 所述

例如喜欢

using System.Linq;

public List<GameObject> cardsToSpawn;
public List<Transform> cardPositions = new List<Transform>();

void Awake()
{
    // First make sure both lists have the same length
    if(cardsToSpawn.Count != cardPositions.Count) 
    {
        Debug.LogError("List length are different! Make sure you have the same amount of elements in both lists!");
        return;
    }

    // get the cards in randomized order
    Random rnd = new Random();
    // OrderBy returns an IEnumerable ordered by the given predicate
    // since our given predicate returns a random value each time
    // the elements get shuffled 
    var shuffledCards = cardsToSpawn.OrderBy(c => rnd.Next()).ToArray();    

    for (int i = 0; i < shuffledCards.Length; i++) 
    {
        // since the cards are already shuffled you can simply iterate
        // over this array in order now
        var card = shuffledCards[i];
        var position = cardPositions[i];

        Instantiate(card, position, transform.rotation);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-06
    • 2018-12-11
    • 2017-07-11
    • 2021-04-19
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 2016-11-19
    相关资源
    最近更新 更多